Solve array modulo and parentheses tasks
SheinSoftware Engineer · OnsiteLast updated
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.
Implement two tasks:
- Count subarrays by modulo remainder
- Input: integer array nums (length n), integers K (1 ≤ K ≤ 10^
- and R (0 ≤ R < K).
- Output: the number of subarrays [i..j] such that (sum(nums[i..j]) mod K) == R.
- Requirements: O(n) time and O(K) space; describe the approach, justify correctness, and analyze complexity. Provide an implementation of countSubarraysByRemainder(nums, K, R).
- Remove the minimum parentheses to balance a string
- Input: string s containing lowercase letters and parentheses '()'.
- Output: any string formed by deleting the minimum number of parentheses so that the result is a valid, balanced parentheses string (characters’ relative order must be preserved).
- Requirements: O(n) time and O(
- extra space beyond the output; describe the algorithm and provide an implementation.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.