**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 :

` ````
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: 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 →## Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

View Solution →## Tree: Huffman Decoding

Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t

View Solution →## Binary Search Tree : Lowest Common Ancestor

You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b

View Solution →