DSA: Queue

For Interview


Progress
Reviewed: 0%
8 Tasks

A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle. It operates similar to a real-world queue, such as people waiting in line for a service. Elements are added to the back of the queue, and removal occurs from the front. The two primary operations are enqueue, which adds an element to the back of the queue, and dequeue, which removes an element from the front. Additionally, a queue supports peek to view the front element without removing it. Queues are commonly used in scenarios like task scheduling, breadth-first search, and handling requests in a sequential manner.


  • Day 1
  • Implement Stack using two Queue

    Concept:



    Resources:



    Assignments:


    Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty).

    Implement the MyStack class:

    void push(int x) Pushes element x to the top of the stack.
    int pop() Removes the element on the top of the stack and returns it.
    int top() Returns the element on the top of the stack.
    boolean empty() Returns true if the stack is empty, false otherwise.

    Example 1:

    Input
    ["MyStack", "push", "push", "top", "pop", "empty"]
    [[], [1], [2], [], [], []]
    Output
    [null, null, null, 2, 2, false]
    Explanation:
    MyStack myStack = new MyStack();
    myStack.push(1);
    myStack.push(2);
    myStack.top(); // return 2
    myStack.pop(); // return 2
    myStack.empty(); // return False



  • Day 2
  • Number of Recent Calls

    Concept:



    Resources:



    Assignments:


    You have a RecentCounter class which counts the number of recent requests within a certain time frame.
    Implement the RecentCounter class:
    RecentCounter() Initializes the counter with zero recent requests.
    int ping(int t) Adds a new request at time t, where t represents some time in milliseconds, and returns the number of requests that has happened in the past 3000 milliseconds (including the new request). Specifically, return the number of requests that have happened in the inclusive range [t - 3000, t].
    It is guaranteed that every call to ping uses a strictly larger value of t than the previous call.

    Input:
    ["RecentCounter", "ping", "ping", "ping", "ping"]
    [[], [1], [100], [3001], [3002]]
    Output
    [null, 1, 2, 3, 3]

    Explanation:
    RecentCounter recentCounter = new RecentCounter();
    recentCounter.ping(1); // requests = [1], range is [-2999,1], return 1
    recentCounter.ping(100); // requests = [1, 100], range is [-2900,100], return 2
    recentCounter.ping(3001); // requests = [1, 100, 3001], range is [1,3001], return 3
    recentCounter.ping(3002); // requests = [1, 100, 3001, 3002], range is [2,3002], return 3



  • Day 3
  • Reveal Cards in Increasing Order

    Concept:



    Resources:



    Assignments:


    You are given an integer array deck. There is a deck of cards where every card has a unique integer. The integer on the ith card is deck[i].
    You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck.
    You will do the following steps repeatedly until all cards are revealed:
    Take the top card of the deck, reveal it, and take it out of the deck.
    If there are still cards in the deck then put the next top card of the deck at the bottom of the deck.
    If there are still unrevealed cards, go back to step 1. Otherwise, stop.
    Return an ordering of the deck that would reveal the cards in increasing order.
    Note that the first entry in the answer is considered to be the top of the deck.

    Example:
    Input: deck = [17,13,11,2,3,5,7]
    Output: [2,13,3,11,5,17,7]

    Explanation:
    We get the deck in the order [17,13,11,2,3,5,7] (this order does not matter), and reorder it.
    After reordering, the deck starts as [2,13,3,11,5,17,7], where 2 is the top of the deck.
    We reveal 2, and move 13 to the bottom. The deck is now [3,11,5,17,7,13].
    We reveal 3, and move 11 to the bottom. The deck is now [5,17,7,13,11].
    We reveal 5, and move 17 to the bottom. The deck is now [7,13,11,17].
    We reveal 7, and move 13 to the bottom. The deck is now [11,17,13].
    We reveal 11, and move 17 to the bottom. The deck is now [13,17].
    We reveal 13, and move 17 to the bottom. The deck is now [17].
    We reveal 17.
    Since all the cards revealed are in increasing order, the answer is correct.



  • Day 4
  • Find Consecutive Integers From A Data Stream

    Concept:



    Resources:



    Assignments:


    For a stream of integers, implement a data structure that checks if the last k integers parsed in the stream are equal to value.

    Implement the DataStream class:

    DataStream(int value, int k) Initializes the object with an empty integer stream and the two integers value and k.
    boolean consec(int num) Adds num to the stream of integers.
    Returns true if the last k integers are equal to value, and false otherwise.
    If there are less than k integers, the condition does not hold true, so returns false.

    Input
    ["DataStream", "consec", "consec", "consec", "consec"]
    [[4, 3], [4], [4], [4], [3]]
    Output
    [null, false, false, true, false]
    Explanation
    DataStream dataStream = new DataStream(4, 3); //value = 4, k = 3
    dataStream.consec(4); // Only 1 integer is parsed, so returns False.
    dataStream.consec(4); // Only 2 integers are parsed. // Since 2 is less than k, returns False.
    dataStream.consec(4); // The 3 integers parsed are all equal to value, so returns True.
    dataStream.consec(3); // The last k integers parsed in the stream are [4,4,3].
    // Since 3 is not equal to value, it returns False.



  • Day 5
  • Number of Students Unable to Eat Lunch

    Concept:



    Resources:



    Assignments:


    The school cafeteria offers circular and square sandwiches at lunch break, referred to by numbers 0 and 1 respectively. All students stand in a queue. Each student either prefers square or circular sandwiches.
    The number of sandwiches in the cafeteria is equal to the number of students. The sandwiches are placed in a stack. At each step:
    If the student at the front of the queue prefers the sandwich on the top of the stack, they will take it and leave the queue.
    Otherwise, they will leave it and go to the queue's end.

    This continues until none of the queue students want to take the top sandwich and are thus unable to eat.
    You are given two integer arrays students and sandwiches where sandwiches[i] is the type of the ith sandwich in the stack (i = 0 is the top of the stack) and students[j] is the preference of the jth student in the initial queue (j = 0 is the front of the queue). Return the number of students that are unable to eat.

    Example
    Input: edges = [[1,2],[2,3],[4,2]]
    Output: 2
    Explanation: As shown in the figure above, node 2 is connected to every other node, so 2 is the center.



  • Day 6
  • Time Needed to Buy Tickets

    Concept:



    Resources:



    Assignments:


    There are n people in a line queuing to buy tickets, where the 0th person is at the front of the line and the (n - 1)th person is at the back of the line.

    You are given a 0-indexed integer array tickets of length n where the number of tickets that the ith person would like to buy is tickets[i].

    Each person takes exactly 1 second to buy a ticket. A person can only buy 1 ticket at a time and has to go back to the end of the line (which happens instantaneously) in order to buy more tickets. If a person does not have any tickets left to buy, the person will leave the line.

    Return the time taken for the person at position k (0-indexed) to finish buying tickets.

    Constraints:
    n == tickets.length
    1 <= n <= 100
    1 <= tickets[i] <= 100
    0 <= k < n

    Example 1:
    Input: tickets = [2,3,2], k = 2
    Output: 6
    Explanation:
    - In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
    - In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
    The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.

    Example 2:
    Input: tickets = [5,1,1,1], k = 0
    Output: 8
    Explanation:
    - In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
    - In the next 4 passes, only the person in position 0 is buying tickets.
    The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.



  • Day 7
  • Find the Winner of the Circular Game

    Concept:



    Resources:



    Assignments:


    There are n friends that are playing a game. The friends are sitting in a circle and are numbered from 1 to n in clockwise order. More formally, moving clockwise from the ith friend brings you to the (i+1)th friend for 1 <= i < n, and moving clockwise from the nth friend brings you to the 1st friend.

    The rules of the game are as follows:

    1. Start at the 1st friend.
    2. Count the next k friends in the clockwise direction including the friend you started at. The counting wraps around the circle and may count some friends more than once.
    3. The last friend you counted leaves the circle and loses the game.
    4. If there is still more than one friend in the circle, go back to step 2 starting from the friend immediately clockwise of the friend who just lost and repeat.
    5. Else, the last friend in the circle wins the game.

    Given the number of friends, n, and an integer k, return the winner of the game.

    Constraints:

    1 <= k <= n <= 500

    Example 1:

    Input: n = 5, k = 2
    Output: 3
    Explanation: Here are the steps of the game:
    1) Start at friend 1.
    2) Count 2 friends clockwise, which are friends 1 and 2.
    3) Friend 2 leaves the circle. Next start is friend 3.
    4) Count 2 friends clockwise, which are friends 3 and 4.
    5) Friend 4 leaves the circle. Next start is friend 5.
    6) Count 2 friends clockwise, which are friends 5 and 1.
    7) Friend 1 leaves the circle. Next start is friend 3.
    8) Count 2 friends clockwise, which are friends 3 and 5.
    9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.

    Example 2:

    Input: n = 6, k = 5
    Output: 1
    Explanation: The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.



×

Let's Go!

Congratulations on getting started. Here is a little reward for you...

×

10

Going to the next task in