Data Structures & AlgorithmsHardTCS NQT · Product Company SDE
Problem: In the array [2, 4, 6, 8, 10], count the number of subarrays whose sum is divisible by 4.
- A. 6
- B. 2
- C. 15
- D. 4
Correct answer: A. 6
The qualifying subarrays are [4], [8], [2,4,6], [6,8,10], [2,4,6,8] and [4,6,8,10], so 6; halving, a sum is ≡ 0 (mod 4) exactly when the halved sum is even. Single elements only give 2, 15 counts all subarrays, and requiring length ≥ 2 gives 4.
More Coding Interview Problems questions
- Problem: A stack-based bracket checker reads the string "([]{()})[" and reports the number of unmatched opening brackets at the end (0 if…
- Problem: An LRU cache of capacity 2 handles requests 1, 2, 1, 3, 2, 1, 4. Each request that is not cached causes a miss and evicts the…
- Problem: Find the number of integer pairs (a, b) with 1 ≤ a ≤ b ≤ 12 such that a and b have the same remainder when divided by 4.
- Problem: Consider the directed graph with arcs 1→2, 2→3, 3→1, 3→4, 4→2, 4→5. How many vertices can reach vertex 5?
- Problem: A string of length 6 is formed using letters only from {a, b}, containing no substring "aa" or "bb". How many such strings are…
- Problem: Compute the number of paths of length exactly 3 from vertex 0 to vertex 2 in the undirected graph with edges 0-1, 1-2, 0-2, 2-3…