Solve array modulo and parentheses tasks
Company: Shein
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Onsite
Quick Answer: This question evaluates proficiency in modular arithmetic and subarray-sum reasoning for remainder counting, together with string manipulation and parentheses-balancing competencies, testing algorithmic problem-solving, correctness justification, and implementation skills in the Coding & Algorithms domain.
Count Subarrays by Modulo Remainder
Constraints
- 1 ≤ K ≤ 10^4
- 0 ≤ R < K
- nums may contain negative integers, zero, and positives
- n (length of nums) can be 0
- Use floored modulo so negatives map into [0, K)
Examples
Input: ([4, 5, 0, -2, -3, 1], 5, 0)
Expected Output: 7
Explanation: There are 7 subarrays whose sum is divisible by 5 (remainder 0).
Input: ([5], 9, 0)
Expected Output: 0
Explanation: The only subarray [5] has sum 5; 5 mod 9 = 5, not 0, so no subarray matches.
Input: ([1, 2, 3], 3, 0)
Expected Output: 3
Explanation: Subarrays [3], [1,2], and [1,2,3] all have sums divisible by 3.
Input: ([2, 7, 11, 15], 5, 2)
Expected Output: 2
Explanation: Two subarrays have a sum whose remainder mod 5 equals 2.
Input: ([], 3, 0)
Expected Output: 0
Explanation: Empty array: there are no subarrays, so the count is 0.
Input: ([-1, -1, -1], 2, 1)
Expected Output: 4
Explanation: Each single -1 (3 of them) and the full subarray sum -3 are odd (remainder 1 mod 2), giving 4.
Input: ([1, 1, 1, 1], 4, 0)
Expected Output: 1
Explanation: Only the full subarray sums to 4, which is divisible by 4.
Hints
- If two prefix sums leave the same remainder mod K, the subarray between them is divisible by K. Generalize this to a target remainder of R.
- For a prefix remainder p, you want earlier prefixes with remainder (p - R) mod K. Count them with a hash map.
- Seed the map with remainder 0 having frequency 1 to account for the empty prefix (subarrays starting at index 0).
Remove Minimum Parentheses to Make Valid
Constraints
- s consists only of lowercase English letters and the characters '(' and ')'
- Letters must never be removed and relative order must be preserved
- s may be empty
- The result must remove the minimum possible number of parentheses
- Any valid minimum-removal result is accepted (the reference uses a deterministic left-to-right rule)
Examples
Input: ('lee(t(c)o)de)',)
Expected Output: 'lee(t(c)o)de'
Explanation: The trailing unmatched ')' is the only paren removed; the rest is already balanced.
Input: ('a)b(c)d',)
Expected Output: 'ab(c)d'
Explanation: The early ')' has no opener and is deleted; '(c)' stays balanced.
Input: ('))((',)
Expected Output: ''
Explanation: Every paren is unmatched: two ')' delete in the forward pass, two '(' in the backward pass.
Input: ('(a(b(c)d)',)
Expected Output: '(a(bc)d)'
Explanation: One surplus '(' remains after matching; it is removed from the right, leaving a balanced string of length 8.
Input: ('',)
Expected Output: ''
Explanation: Empty input yields empty output.
Input: ('abc',)
Expected Output: 'abc'
Explanation: No parentheses to remove; the string is returned unchanged.
Input: ('()',)
Expected Output: '()'
Explanation: Already valid; nothing is removed.
Input: ('(((',)
Expected Output: ''
Explanation: All three '(' are unmatched and removed in the backward pass.
Input: (')))',)
Expected Output: ''
Explanation: All three ')' are unmatched and removed in the forward pass.
Hints
- Track the number of currently unmatched '('. A ')' that arrives when this count is 0 can never be matched, so delete it.
- After one pass, whatever value the open counter holds is the number of '(' that were never closed — those must be removed too.
- Remove the surplus '(' by scanning from the right so the deletions land on the last unmatched opens; total deletions are provably minimal.