Revolving Door - Amazon Top Interview Questions


Problem Statement :


You are given a list of list of integers requests. requests[i] contains [time, direction] meaning at time time, a person arrived at the door and either wanted to go in (1) or go out (0).

Given that there's only one door and it takes one time unit to use the door, we have the following rules to resolve conflicts:

The door starts with in position and then is set to the position used by the last person.
If there's only one person at the door at given time, they can use the door.
When two or more people want to go in, earliest person goes first and then the direction previously used holds precedence.
If no one uses the door for one time unit, it reverts back to the starting in position.
Return a sorted list of lists where each element contains [time, direction], meaning at time, a person either went in or out.

Constraints

0 ≤ n ≤ 100,000 where n is the length of requests

Example 1

Input

requests = [
    [1, 0],
    [2, 1],
    [5, 0],
    [5, 1],
    [2, 0]
]

Output

[
    [1, 0],
    [2, 0],
    [3, 1],
    [5, 1],
    [6, 0]
]

Explanation

The door starts as in

At time 1, there's only one person so they can go out. Door becomes out.

At time 2, there's two people but the person going out has priority so they go out.

At time 3, the person looking to go in can now go in.

At time 5, there's two people but the person going in has priority so they go out.

At time 6, the last person can go out.

Example 2

Input

requests = [
    [1, 0],
    [2, 0],
    [2, 1],
    [1, 1]
]

Output

[
    [1, 1],
    [2, 0],
    [3, 0],
    [4, 1]
]

Explanation

The door starts as in

At time 1, there's two people but the person going in has higher priority.

At time 2, the other person who came at time 1 can go out. Door becomes out

At time 3, there's two people but the person going out has higher priority.

At time 4, the last person can go in



Solution :



title-img




                        Solution in C++ :

vector<vector<int>> solve(vector<vector<int>> &requests) {
    int n = requests.size();
    vector<vector<int>> finalState;

    sort(requests.begin(), requests.end(),
         [&](vector<int> &v1, vector<int> &v2) { return v1[0] < v2[0]; });

    int gateDirection = 1;
    int time = -1;

    vector<vector<int>> currentRequests;

    int left = 0;
    int right = 0;

    while (right < n) {
        if (requests[left][0] - time >= 1) gateDirection = 1;
        if (time < requests[left][0]) time = requests[left][0];
        while (right < n && requests[right][0] == requests[left][0]) {
            currentRequests.push_back(requests[right]);
            right++;
        }

        while (!currentRequests.empty()) {
            sort(currentRequests.begin(), currentRequests.end(),
                 [&](vector<int> &v1, vector<int> &v2) {
                     if (v1[0] == v2[0]) {
                         if (gateDirection == v1[1])
                             return false;
                         else if (gateDirection == v2[1])
                             return true;
                     }
                     return v1[0] > v2[0];
                 });

            finalState.push_back({time, currentRequests.back()[1]});
            gateDirection = currentRequests.back()[1];
            currentRequests.pop_back();
            time++;
        }

        left = right;
    }
    return finalState;
}
                    


                        Solution in Java :

import java.util.*;

class Solution {
    public int[][] solve(int[][] requests) {
        // Create and populate Queues for in- and out-going times
        PriorityQueue<Integer> inQueue = new PriorityQueue<>();
        PriorityQueue<Integer> outQueue = new PriorityQueue<>();
        for (int i = requests.length - 1; i >= 0; i--) {
            if (requests[i][1] == 0) {
                outQueue.add(requests[i][0]);
            } else {
                inQueue.add(requests[i][0]);
            }
        }

        // Build result array
        int[][] result = new int[requests.length][2];
        int time = -1;
        int door = 1;
        for (int i = 0; i < requests.length; i++) {
            // inQueue empty or outQueue item smaller
            if (inQueue.size() == 0 || outQueue.size() != 0 && outQueue.peek() < inQueue.peek()) {
                time = Math.max(time + 1, outQueue.poll());
                result[i] = new int[] {time, 0};
                door = 0;
            }
            // outQueue empty or inQueue item smaller
            else if (outQueue.size() == 0 || inQueue.peek() < outQueue.peek()) {
                time = Math.max(time + 1, inQueue.poll());
                result[i] = new int[] {time, 1};
                door = 1;
            }
            // equal items in both queues
            else {
                if (inQueue.peek() - time > 1) {
                    door = 1;
                }
                if (door == 1) {
                    time = Math.max(time + 1, inQueue.poll());
                    result[i] = new int[] {time, 1};
                } else {
                    time = Math.max(time + 1, outQueue.poll());
                    result[i] = new int[] {time, 0};
                }
            }
        }
        return result;
    }
}
                    


                        Solution in Python : 
                            
class Solution:
    def solve(self, requests):
        requests.sort()
        i, j = 0, 1
        while i < len(requests):
            if j < len(requests) and requests[i][0] == requests[j][0]:
                j += 1
                continue

            if not i or requests[i][0] - requests[i - 1][0] > 1:
                prev_direc = 1
            else:
                prev_direc = requests[i - 1][1]

            requests[i:j] = sorted(requests[i:j], reverse=prev_direc)

            if i and requests[i][0] <= requests[i - 1][0]:
                requests[i][0] = requests[i - 1][0] + 1
            for k in range(i + 1, j):
                requests[k][0] = requests[k - 1][0] + 1

            i = j
            j = i + 1

        return requests
                    


View More Similar Problems

Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's

View Solution →

Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →

Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →

Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →

Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

View Solution →

Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

View Solution →