Rectangle Collection With Undo
Updated
Problem statement
You keep a collection of rectangles. It starts empty. Every rectangle has sides parallel to the x and y axes. Coordinates and borders are inclusive, so a point on the edge of a rectangle is inside it. A rectangle is active while it is in the collection.
You get a list of operations. Apply them in order:
ADD id x1 y1 x2 y2adds a rectangle with that ID. If an active rectangle already has that ID, the add is invalid and does nothing.REMOVE idremoves the active rectangle with that ID. If no active rectangle has that ID, the remove is invalid and does nothing.REMOVE_AT x ylooks at the active rectangles that contain the point. If there are any, it removes the one added most recently.REMOVE_ALL x yremoves every active rectangle that contains the point.UNDOreverses the most recent successful change that has not already been undone.
Some operations do nothing. These are an invalid add or remove, a point removal that finds no rectangle, and an undo with nothing left to undo. They leave both the collection and the undo history unchanged.
After each operation, take the IDs of the active rectangles. Sort them in lexicographic order and join them with commas. If no rectangle is active, use EMPTY instead. Return one such string per operation.
Function
runRectangleCollection(operations: String[]) → String[]Examples
Example 1
operations = ["ADD a 0 0 4 4","ADD b 2 2 6 6","REMOVE_AT 3 3","UNDO","REMOVE_ALL 3 3"]return = ["a","a,b","a","a,b","EMPTY"]- After the two adds,
aandbare both active. - The point removal hits both rectangles. It removes
b, the one added later. UNDObringsbback.- The batch removal then removes both, so the last result is
EMPTY.
Example 2
operations = ["ADD box -1 -1 1 1","REMOVE missing","REMOVE_AT 5 5","UNDO","UNDO"]return = ["box","box","box","EMPTY","EMPTY"]Removing missing fails, and the point removal finds no rectangle. Neither one changes anything, so neither enters the undo history. The first UNDO reverses the add, which empties the collection. The second UNDO has nothing left to undo, so the result stays EMPTY.
Constraints
1 <= operations.length <= 2000- IDs are nonempty strings of letters and digits. Two active rectangles never share an ID.
-10^9 <= x1 <= x2 <= 10^9, and the same limits hold for y.- Every operation has exactly the fields shown above.
More OpenAI Phone Screen questions
- Secret Number Search with One-Call-Delayed FeedbackPHONE SCREEN · Seen Nov 2026
- Determine the Winner of Three-Card HandsPHONE SCREEN · Seen Nov 2026
- Grid Infection with Recovery After D DaysPHONE SCREEN · Seen Nov 2026
- Sparse Plant Infection SimulationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Light Source Hits: Sample, Compare, and FitPHONE SCREEN · Seen Nov 2026
- Rectangle Point Coverage CountsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Adaptive Calibration ErrorPHONE SCREEN · Seen Nov 2026
- Elevator Scheduling SimulationPHONE SCREEN · Seen Nov 2026
- Evaluate Noisy AnnotatorsPHONE SCREEN · Seen Oct 2026
- Network Endpoint or Cycle BoundaryOA · PHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Split Tender Part 4 - RefundsStripe · OA · PHONE SCREENStripe interview questions
- Design In-Memory File SystemSnowflake · PHONE SCREENSnowflake interview questions
- Dynamic Least-Loaded Job DispatcherCresta · PHONE SCREENCresta interview questions
- Sparse Vector and Matrix OperationsBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Alternating Direction Jump GameGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Capacity-Limited Timed CacheNetflix · PHONE SCREENNetflix interview questions
- Tiered Expiring Item StoreHudson River Trading · PHONE SCREEN · ONSITE INTERVIEWHudson River Trading interview questions
- LRU Cache with Expiring EntriesPaytm · PHONE SCREENPaytm interview questions
- LFU CacheFreshworks · PHONE SCREENFreshworks interview questions
- Strong and Weak Reference LifecycleSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions