Data Structures & AlgorithmsVery hardGoogle · Arrays
How many contiguous subarrays of [4, 5, 0, -2, -3, 1] have a sum divisible by 5?
- A. 5
- B. 6
- C. 8
- D. 7
Correct answer: D. 7
Prefix sums mod 5 are 0, 4, 4, 4, 2, 4, 0 (counting the empty prefix); pairs of equal residues give 1 + 6 = 7 subarrays.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- How many inversions (pairs i < j with a[i] > a[j]) does [8, 4, 2, 1, 7, 3, 6, 5] have?
- Eight trains have arrival times 900, 915, 930, 945, 1000, 1010, 1030, 1100 and departure times (in some order) 930, 1000, 1015, 1000…
- A hash table of size 7 with h(k) = k mod 7 and linear probing holds keys 3, 10, 17, 24 at indices 3, 4, 5, 6. Key 10 is then deleted by…
- How many pairs of positions (i < j) in [5, 3, 7, 9, 1, 2, 6, 10, 14, 4] have a[i] + a[j] divisible by 4?
- A greedy stack algorithm removes exactly 3 digits from the number string "1432219" to get the smallest possible number (keeping the order…
- A cache with 3 slots serves the page requests 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5. How many misses occur under LRU replacement and under…