Coding questions · SoFi
Solve Time-Window and Binary Swap Problems
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.
This online assessment contained two coding problems:
-
Maximum requests in a time window You are given a non-decreasing integer array
requestTimes, whererequestTimes[i]is the second at which thei-th request arrives, and an integerwindowSize. Return the maximum number of requests that occur within any inclusive time interval of lengthwindowSizeseconds. In other words, find the largest number of elements that can fit inside some interval[t, t + windowSize]. -
Simultaneous swaps in a binary string You are given a binary string
sconsisting only of'0'and'1'. Every second, all adjacent occurrences of'01'are swapped simultaneously to become'10'. Repeat this process until the string no longer contains'01'. Return the number of seconds required.
Example: s = "001011" transforms as follows:
001011 -> 010101 -> 101010 -> 110100 -> 111000
So the answer is 4 seconds.
Part 1: Maximum Requests in a Time Window
You are given a non-decreasing integer array requestTimes, where requestTimes[i] is the second at which the i-th request arrives, and a non-negative integer windowSize.
Return the maximum number of requests that occur within any inclusive time interval of length windowSize seconds. In other words, find the largest number of values that can fit inside some interval [t, t + windowSize].
Because the interval is inclusive, requests at both endpoints count.
Constraints
0 <= len(requestTimes) <= 2000000 <= requestTimes[i] <= 10^9requestTimesis sorted in non-decreasing order0 <= windowSize <= 10^9
Examples
Example 1
Input: ([1, 1, 2, 3, 7, 8], 2)
Output: 4
Explanation: The interval [1, 3] contains 1, 1, 2, 3, so the answer is 4.
Example 2
Input: ([], 5)
Output: 0
Explanation: There are no requests, so the maximum count is 0.
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
You are given a binary string s containing only '0' and '1'.
Every second, all adjacent occurrences of '01' are swapped simultaneously, turning each such pair into '10'. This process repeats until the string no longer contains '01'.
Return the number of seconds required.
Example:
001011 -> 010101 -> 101010 -> 110100 -> 111000
So the answer is 4.
Constraints
0 <= len(s) <= 200000s[i]is either'0'or'1'
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.