Implement Calendar Layout and String Packing
Company: Robinhood
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates algorithmic problem-solving skills related to interval overlap handling and constrained packing, measuring competency in mapping temporal events to minimal column layouts and assigning strings into fixed-width rows under capacity constraints.
Part 1: Day-View Calendar Layout
Constraints
- 0 <= len(events) <= 200000
- 0 <= start < end <= 1440
- Events that touch at a boundary, such as (60, 120) and (120, 180), do not overlap.
Examples
Input: ([(60, 120), (90, 150), (150, 180)],)
Expected Output: [(0, 2), (1, 2), (0, 1)]
Explanation: The first two events overlap, so they need 2 columns. The third starts exactly when the second ends, so it forms a new group with 1 column.
Input: ([(0, 30), (10, 40), (20, 50), (60, 70)],)
Expected Output: [(0, 3), (1, 3), (2, 3), (0, 1)]
Explanation: The first three events overlap in one group and reach a peak concurrency of 3. The last event is separate.
Input: ([(30, 60), (0, 15), (10, 40), (45, 50)],)
Expected Output: [(0, 2), (0, 2), (1, 2), (1, 2)]
Explanation: All four events belong to one connected overlap group, and the maximum simultaneous overlap is 2.
Input: ([],)
Expected Output: []
Explanation: No events means no layout.
Hints
- Sort events by start time and track currently active events with a min-heap ordered by end time.
- Whenever the active set becomes empty, one overlap group has finished, so you can finalize its total column count.
Part 2: Place Strings into Three Rows - Any Valid Placement
Constraints
- 0 <= len(strings) <= 200000
- 0 <= W <= 1000000000
- There are exactly 3 rows indexed 0, 1, and 2
- The width of a string is len(string)
Examples
Input: (['ab', 'c', 'def', 'gh'], 4)
Expected Output: ([0, 0, 1, 2], [1, 1, 2])
Explanation: Use first-fit: 'ab' and 'c' go to row 0, 'def' to row 1, and 'gh' to row 2.
Input: (['aaaa', 'bbbb', 'cccc', 'd'], 4)
Expected Output: None
Explanation: The first three strings exactly fill the three rows, so the last string cannot be placed.
Input: ([], 5)
Expected Output: ([], [5, 5, 5])
Explanation: No strings are placed, so all rows keep their full width.
Input: (['', 'ab', '', 'c'], 2)
Expected Output: ([0, 0, 0, 1], [0, 1, 2])
Explanation: Empty strings consume zero width and still follow the first-fit rule.
Hints
- Because there are only 3 rows, you can check all rows for every string in constant time.
- Keep an array of remaining capacities and update it as you place strings.
Part 3: Place Strings into Three Rows - Most Remaining Space
Constraints
- 0 <= len(strings) <= 200000
- 0 <= W <= 1000000000
- There are exactly 3 rows indexed 0, 1, and 2
- The width of a string is len(string)
Examples
Input: (['aa', 'b', 'cc', 'd'], 3)
Expected Output: ([0, 1, 2, 1], [1, 1, 1])
Explanation: Each step chooses the row with the most free space among those that can fit the current string.
Input: (['aaa', 'bbb', 'ccc', 'd'], 3)
Expected Output: None
Explanation: The first three strings fill the three rows completely, so the last string cannot be placed.
Input: ([], 2)
Expected Output: ([], [2, 2, 2])
Explanation: No strings means nothing is placed.
Input: (['a', 'a', 'a', 'a'], 2)
Expected Output: ([0, 1, 2, 0], [0, 1, 1])
Explanation: The first three strings spread across the rows, then the final tie on remaining space is broken by choosing the smallest row index.
Hints
- For each string, compare the remaining capacities of all rows that can fit it before you subtract the string length.
- You can enforce the tie-break naturally by scanning rows from 0 to 2 and only replacing the current best when you find a strictly larger remaining capacity.