Data Structures & AlgorithmsHardTCS NQT · Product Company SDE
Problem: How many contiguous subarrays of [3, -1, 4, -2, 5, -6, 2, 1] have sum at most 6 when the sum is computed over all non-empty contiguous subarrays?
- A. 31
- B. 32
- C. 34
- D. 36
Correct answer: C. 34
Brute force over all 36 subarrays finds 34 with sum ≤ 6. Using < 6 gives 31, a shrinking two-pointer window gives 32 because negative values break its monotonicity, and 36 counts every subarray.
More Coding Interview Problems questions
- Problem: Longest substring without repeating characters of "pwwkewabcdbbxq". What is its length?
- Problem: Build a binary search tree by inserting 50, 30, 70, 20, 40, 60, 80, 35, 45, 65 in that order (duplicates are not inserted). Count…
- Problem: Starting from an empty tree of directed edges, a DAG has edges 0→1, 0→2, 1→3, 2→3, 2→4, 3→5, 4→5, 1→4. How many distinct directed…
- Problem: An MST is built with Kruskal's algorithm on vertices 0–4 and edges: 0-1 (4), 0-2 (1), 1-2 (2), 1-3 (5), 2-3 (8), 3-4 (3), 2-4…
- Problem: How many pairs (i, j) with i < j in the array [4, 8, 1, 5, 9, 2, 7, 3, 6] satisfy a[j] − a[i] = 3?
- Problem: How many contiguous subarrays of [7, 2, 9, 4, 1, 8, 3, 5, 6, 10] have a sum divisible by 7?