Count subarrays with odd zero count
Company: SIG (Susquehanna)
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Take-home Project
Quick Answer: This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Count subarrays with odd zero count states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Count Subarrays With an Odd Number of Zeros
Constraints
- 0 <= nums.length <= 10^5
- Array elements are arbitrary integers; only whether each element equals 0 matters.
- The answer can exceed 32-bit range for large inputs (up to ~n^2/2 subarrays), so use a 64-bit accumulator.
Examples
Input: ([0, 0, 1, 0, 1],)
Expected Output: 9
Explanation: Of the 15 contiguous subarrays, 9 contain an odd number of zeros. (The original prompt's claim of 4 was a miscount.)
Input: ([],)
Expected Output: 0
Explanation: Empty array has no subarrays.
Input: ([0],)
Expected Output: 1
Explanation: The single subarray [0] has one zero (odd).
Input: ([1, 1, 1],)
Expected Output: 0
Explanation: No zeros anywhere, so no subarray has an odd zero count.
Input: ([0, 0, 0],)
Expected Output: 4
Explanation: Subarrays with odd zero counts: [0]x3 (each 1 zero) and the full [0,0,0] (3 zeros) = 4.
Input: ([1, 0, 1, 0, 1],)
Expected Output: 8
Explanation: 8 of the 15 subarrays contain an odd number of zeros.
Hints
- Brute force over all O(n^2) subarrays works but is too slow for large n; you need O(n).
- Only the parity (even/odd) of the zero count matters. Track a running parity bit as you scan.
- A subarray has an odd number of zeros iff the prefix parities at its two endpoints differ. Keep counts of how many prefixes had even vs odd parity (start with one even prefix), and add the opposite-parity count at each step.
Minute Difference Between Two HH:MM Times
Constraints
- Each time is a valid 'HH:MM' string with HH in [00, 23] and MM in [00, 59].
- t1 and t2 are on the same day, and t2 >= t1, so the result is non-negative (0 to 1439).
- Leading zeros are present (e.g. '09:05').
Examples
Input: ("12:23", "13:24")
Expected Output: 61
Explanation: 12:23 = 743 min, 13:24 = 804 min; 804 - 743 = 61.
Input: ("00:00", "00:00")
Expected Output: 0
Explanation: Identical times differ by 0 minutes.
Input: ("00:00", "23:59")
Expected Output: 1439
Explanation: Midnight to one minute before the next midnight = 1439 minutes.
Input: ("09:05", "09:50")
Expected Output: 45
Explanation: Same hour: 50 - 5 = 45 minutes.
Input: ("08:30", "10:00")
Expected Output: 90
Explanation: 08:30 = 510 min, 10:00 = 600 min; 600 - 510 = 90.
Hints
- Split each string on the ':' character to get the hour and minute parts.
- Convert each time to a single integer: total minutes from midnight = hours * 60 + minutes.
- Subtract the two totals. Because t2 >= t1, no wrap-around handling is needed.
Rotate a Matrix 90 Degrees Clockwise
Constraints
- The matrix is square: n x n, with 0 <= n <= 100.
- Matrix entries are integers (may be negative).
- Return a new matrix; an in-place solution is also acceptable but the reference returns a fresh grid.
Examples
Input: ([[1, 2, 3], [4, 5, 6], [7, 8, 9]],)
Expected Output: [[7, 4, 1], [8, 5, 2], [9, 6, 3]]
Explanation: The first column (7,4,1) becomes the top row, etc.
Input: ([[1]],)
Expected Output: [[1]]
Explanation: A 1x1 matrix is unchanged by rotation.
Input: ([[1, 2], [3, 4]],)
Expected Output: [[3, 1], [4, 2]]
Explanation: Clockwise: bottom-left 3 moves to top-left, etc.
Input: ([[-1, -2], [-3, -4]],)
Expected Output: [[-3, -1], [-4, -2]]
Explanation: Negative values rotate identically.
Input: ([],)
Expected Output: []
Explanation: An empty matrix rotates to an empty matrix.
Hints
- Think about where a single element ends up: the element at (i, j) lands at (j, n-1-i) after a clockwise turn.
- Equivalently, rotating clockwise = transpose the matrix, then reverse each row.
- Allocate an n x n result grid and copy each element to its rotated position; handle the empty matrix (n = 0) as a base case.