Solve sliding-window, flattening, decode, and O(1) random set
Company: Bloomberg
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This multi-part question evaluates competence in string processing and nested parsing, linked-list flattening and recursion depth management, dynamic set design for O(1) operations, and algorithmic complexity analysis with attention to edge-case handling.
Part 1: Longest Substring Without Repeating Characters
Constraints
- 0 <= len(s) <= 100000
- s may contain any Unicode characters.
- The answer for the empty string is 0.
- The target time complexity is O(n).
Examples
Input: ("",)
Expected Output: 0
Explanation: The empty string has no substrings.
Input: ("abcabcbb",)
Expected Output: 3
Explanation: The longest substrings without repeats include abc.
Input: ("bbbbb",)
Expected Output: 1
Explanation: Every valid substring can contain only one b.
Input: ("pwwkew",)
Expected Output: 3
Explanation: The longest valid substring is wke.
Input: ("你好你abc",)
Expected Output: 5
Explanation: The longest valid substring is 好你abc, length 5.
Hints
- Use a sliding window whose left boundary only moves forward.
- Store the most recent index where each character appeared so you can skip past duplicates quickly.
Part 2: Flatten a Linked List with Down Pointers
Constraints
- 0 <= len(nodes) <= 100000
- Each node entry has exactly three integers: [val, next_index, down_index].
- next_index and down_index are either -1 or valid indices into nodes.
- The reachable structure from head is acyclic.
- Node values may repeat.
Examples
Input: ([], -1)
Expected Output: []
Explanation: An empty structure flattens to an empty list.
Input: ([[7, -1, -1]], 0)
Expected Output: [7]
Explanation: A single node with no children remains unchanged.
Input: ([[1, 1, 2], [2, -1, -1], [3, 3, -1], [4, -1, -1]], 0)
Expected Output: [1, 3, 4, 2]
Explanation: Node 1's down chain 3 -> 4 is visited before node 2.
Input: ([[1, 1, 3], [2, 2, 6], [3, -1, -1], [4, 4, -1], [5, -1, 5], [6, -1, -1], [7, -1, -1]], 0)
Expected Output: [1, 4, 5, 6, 2, 7, 3]
Explanation: Nested down pointers are recursively flattened before returning to the original next chain.
Hints
- Depth-first order means down is processed before the original next.
- An explicit stack can avoid recursion-depth issues: push next first, then down, so down is popped first.
Part 3: Lottery Set with O(1) Insert, Remove, and Random Access
Constraints
- 0 <= len(operations) <= 100000
- Items x are integers.
- At any time, the set contains unique items only.
- Operation [3, i] is only called when the set is non-empty.
- For operation [3, i], 0 <= i < current set size under the required array-backed implementation.
Examples
Input: ([] ,)
Expected Output: []
Explanation: No operations produce no outputs.
Input: ([[1, 10], [1, 20], [3, 0], [2, 10], [3, 0]],)
Expected Output: [1, 1, 10, 1, 20]
Explanation: After removing 10, 20 is swapped into index 0.
Input: ([[1, 5], [1, 5], [2, 6], [2, 5]],)
Expected Output: [1, 0, 0, 1]
Explanation: Duplicate insertion and removing a missing item fail.
Input: ([[1, 1], [1, 2], [1, 3], [2, 2], [3, 1], [1, 4], [3, 2]],)
Expected Output: [1, 1, 1, 1, 3, 1, 4]
Explanation: Removing 2 swaps 3 into its slot, so index 1 returns 3.
Input: ([[1, 7], [2, 7], [1, 8], [3, 0]],)
Expected Output: [1, 1, 1, 8]
Explanation: Removing the last remaining item is handled correctly.
Hints
- Keep items in a list and maintain a dictionary mapping each item to its current list index.
- To remove in O(1), swap the item with the last list element, update the moved element's index, then pop.
Part 4: Decode a Nested Encoded String
Constraints
- 0 <= len(s) <= 100000
- The input string is valid.
- Repeat counts k are positive integers and may have multiple digits.
- The decoded output length is at most 1000000.
- Digits appear only as repeat counts before brackets.
Examples
Input: ("",)
Expected Output: ""
Explanation: The empty string decodes to itself.
Input: ("3[a]2[bc]",)
Expected Output: "aaabcbc"
Explanation: Three a characters followed by two bc blocks.
Input: ("3[a2[c]]",)
Expected Output: "accaccacc"
Explanation: The nested block a2[c] becomes acc, repeated three times.
Input: ("12[z]",)
Expected Output: "zzzzzzzzzzzz"
Explanation: Multi-digit repeat counts must be parsed correctly.
Input: ("2[abc]3[cd]ef",)
Expected Output: "abcabccdcdcdef"
Explanation: Encoded blocks and plain trailing characters are both supported.
Hints
- When you see an opening bracket, push the current partial string and repeat count onto a stack.
- When you see a closing bracket, decode the current segment and append it to the previous partial string.