Solve Time-Window and Binary Swap Problems
Company: SoFi
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: This assessment evaluates algorithmic proficiency in time-based counting and synchronous string transformation, covering competencies in windowed aggregation, array and binary-string processing, simulation of concurrent swaps, and algorithmic complexity analysis.
Part 1: Maximum Requests in a Time Window
Constraints
- `0 <= len(requestTimes) <= 200000`
- `0 <= requestTimes[i] <= 10^9`
- `requestTimes` is sorted in non-decreasing order
- `0 <= windowSize <= 10^9`
Examples
Input: ([1, 1, 2, 3, 7, 8], 2)
Expected Output: 4
Explanation: The interval `[1, 3]` contains `1, 1, 2, 3`, so the answer is 4.
Input: ([], 5)
Expected Output: 0
Explanation: There are no requests, so the maximum count is 0.
Input: ([4, 4, 4, 4], 0)
Expected Output: 4
Explanation: All requests happen at the same second, and an interval of length 0 can include all of them.
Input: ([1, 2, 4, 4, 5, 9], 1)
Expected Output: 3
Explanation: The interval `[4, 5]` contains `4, 4, 5`.
Input: ([10], 100)
Expected Output: 1
Explanation: A single request always fits in the window.
Hints
- Since the array is already sorted, try maintaining a valid range with two pointers.
- For a fixed right endpoint, move the left pointer forward until `requestTimes[right] - requestTimes[left] <= windowSize`.
Part 2: Seconds to Eliminate All '01' Pairs
Constraints
- `0 <= len(s) <= 200000`
- `s[i]` is either `'0'` or `'1'`
Examples
Input: ("001011",)
Expected Output: 4
Explanation: Following the simultaneous swaps gives 4 steps before the string becomes `111000`.
Input: ("",)
Expected Output: 0
Explanation: An empty string has no `'01'` pairs.
Input: ("1111",)
Expected Output: 0
Explanation: There is no `'01'` substring, so no swaps are needed.
Input: ("01",)
Expected Output: 1
Explanation: After one simultaneous swap, `01` becomes `10`.
Input: ("000111",)
Expected Output: 5
Explanation: Although each `1` must cross 3 zeros, later `1`s are delayed by earlier ones, so the total time is 5.
Hints
- A direct simulation works, but can be too slow for long strings.
- Track how many zeros have appeared before each `'1'`. A `'1'` may need to wait not only for zeros, but also for earlier `'1'` characters moving through the same zeros.