Implement text wrapping, waitlist, and intervals
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates skills in string parsing and word-wrapping logic, data-structure design for FIFO queue management and deletions, and interval reasoning for overlap detection and resource allocation in the Coding & Algorithms domain.
Part 1: Text wrapping - count rendered lines in a fixed-width document
Constraints
- 1 <= maxWidth <= 10^4
- 0 <= len(text) <= 10^5
- text contains printable characters, spaces, and newline characters
Examples
Input: ('Hello world', 6)
Expected Output: 2
Explanation: 'Hello' fits on the first line and 'world' goes on the second.
Input: ('a bb ccc', 6)
Expected Output: 2
Explanation: Multiple spaces collapse to one separator, so the lines are 'a bb' and 'ccc'.
Input: ('abcdefghij', 4)
Expected Output: 3
Explanation: The long word is split into 'abcd', 'efgh', and 'ij'.
Input: (' hi there\n\nok ', 5)
Expected Output: 4
Explanation: First segment wraps to 2 lines, the empty middle segment contributes 1 blank line, and 'ok' uses 1 line.
Input: ('', 10)
Expected Output: 0
Explanation: An empty string renders no lines.
Hints
- Split the input on newline first; each segment is wrapped independently.
- For one segment, use segment.split() to get words, then simulate greedy packing onto lines.
Part 2: Waitlist APIs - join, delete, and find earliest matching party
Constraints
- 1 <= len(operations) <= 2 * 10^5
- 1 <= partySize, tableSize <= 10^9 for valid operations
- userId values are hashable and are strings in the tests
Examples
Input: [('join', 'alice', 4), ('join', 'bob', 2), ('find_first_match', 2), ('find_first_match', 4)]
Expected Output: [True, True, 'bob', 'alice']
Explanation: For table size 2, only 'bob' fits. For table size 4, the earliest fitting party is 'alice'.
Input: [('join', 'x', 2), ('delete', 'x'), ('join', 'x', 4), ('find_first_match', 3), ('find_first_match', 4)]
Expected Output: [True, True, True, None, 'x']
Explanation: After deletion, 'x' can rejoin as a new entry.
Input: [('join', 'u1', 3), ('join', 'u1', 2), ('delete', 'u2'), ('find_first_match', 1)]
Expected Output: [True, False, False, None]
Explanation: Duplicate active joins fail, deleting a missing user fails, and no party fits table size 1.
Input: [('find_first_match', 4), ('join', 'a', 5), ('join', 'b', 2), ('delete', 'a'), ('find_first_match', 4)]
Expected Output: [None, True, True, True, 'b']
Explanation: The first query is on an empty waitlist; after deleting 'a', 'b' becomes the first match.
Input: [('join', 'bad', 0), ('join', 'ok', 1), ('find_first_match', 0)]
Expected Output: [False, True, None]
Explanation: A non-positive party size is invalid, and table size 0 matches nobody.
Hints
- Because joins only append, you can assign each join a permanent increasing index.
- A segment tree storing the minimum active party size in each range can help you find the leftmost active party with size <= tableSize.
Part 3: Intervals - determine whether two half-open intervals overlap
Constraints
- -10^9 <= start <= end <= 10^9
- Each interval is half-open: [start, end)
Examples
Input: ((1, 5), (4, 8))
Expected Output: True
Explanation: They share times in [4, 5).
Input: ((1, 2), (2, 3))
Expected Output: False
Explanation: They only touch at time 2, which is excluded from the first interval.
Input: ((0, 0), (0, 10))
Expected Output: False
Explanation: [0, 0) is empty.
Input: ((-3, 1), (-1, 0))
Expected Output: True
Explanation: The second interval lies inside the first.
Hints
- Two half-open intervals overlap exactly when the later start is strictly less than the earlier end.
- Be careful with touching endpoints like [1, 2) and [2, 3).
Part 4: Intervals - minimum number of conference rooms required
Constraints
- 0 <= len(intervals) <= 2 * 10^5
- -10^9 <= start < end <= 10^9
- Intervals are half-open: [start, end)
Examples
Input: []
Expected Output: 0
Explanation: No meetings require no rooms.
Input: [(0, 30), (5, 10), (15, 20)]
Expected Output: 2
Explanation: One room can host (0, 30) and another handles the shorter overlapping meetings.
Input: [(7, 10), (2, 4)]
Expected Output: 1
Explanation: The meetings do not overlap.
Input: [(1, 2), (2, 3), (3, 4)]
Expected Output: 1
Explanation: Touching endpoints do not overlap for half-open intervals.
Input: [(1, 5), (2, 6), (3, 7), (4, 8)]
Expected Output: 4
Explanation: All four meetings overlap around time 4.
Hints
- Sort meetings by start time.
- Use a min-heap of current room end times; if the smallest end time is <= the next start time, that room can be reused.