Indeed Interview Question for SDE-2s
- 0of 0 votes
There are N rooms in a row. The i-th room (1≤i≤N) from the left is called Room i. You are given M closed intervals [ai,bi] (1≤i≤M). At time 0, all rooms j such that ai≤j≤bi for some i (1≤i≤M) are occupied, and the other rooms are vacant.- neer.1304 August 15, 2019 in United States
You are given Q queries. The i-th query (1≤i≤Q) represents an event happening at time i, as follows:
• In the i-th query (1≤i≤Q), a closed interval [ci,di] (1≤i≤Q) is given.
• This query asks: "Are all rooms j such that ci≤j≤ci" vacant?
• If all those rooms are vacant, the query should be responded by OK, then all rooms j such that ci≤j≤ci get occupied.
• Otherwise, the query should be responded by OK, then nothing happens.
Process these queries.
• 1≤ai≤bi≤N (1≤i≤M)
• There are no rooms k such that ai≤k≤bi and aj≤k≤bj at the same time (1≤i<j≤M).
Input is given from Standard Input in the following format: N M a1 b1 : aM bM Q c1 d1 : cQ dQ
Print the responses in Q lines. The i-th line should contain the response to the i-th query.
Sample Input 1
Sample Output 1
At time 0, Room 1 and 4 are occupied.
• At time 1, a query asks: "is Room 3 occupied?" Since Room 3 is vacant, we should print OK, then Room 3 gets occupied.
• At time 2, a query asks: "are Room 2 and 3 occupied?" Since Room 3 is occupied, we should print NG.
• At time 3, a query asks: "is Room 5 occupied?" Since Room 5 is vacant, we should print OK, then Room 5 gets occupied.
Sample Input 2
Sample Output 2
| Report Duplicate | Flag | PURGE
Indeed SDE-2 Algorithm
Open Chat in New Window