Data Structures & AlgorithmsModerateTCS NQT · Accenture
Problem: In the sorted array [1, 2, 4, 7, 8, 11, 13], how many pairs (i, j) with i < j have a[j] − a[i] ≤ 2?
- A. 5
- B. 3
- C. 6
- D. 4
Correct answer: D. 4
Because the array is sorted, the differences grow with the gap, so a two-pointer sweep works. Checking only adjacent elements misses pairs such as (2, 4) that are not neighbours.
More Coding Interview Problems questions
- Problem: Piles of bananas are [3, 6, 7, 11] and you must finish all of them within 8 hours. Each hour you eat up to k bananas from one…
- Problem: Intervals [1,4], [3,8], [6,9], [10,12], [2,5] are given. How many of them contain the point 5 (endpoints included)?
- Problem: Compute (1³ + 2³ + … + 100³) modulo 1000.
- Problem: A trie stores the words "apple", "app", "apply", "ape", "banana". How many stored words start with the prefix "app" (including a…
- Problem: Subtraction game: from n stones a player may remove 1, 3 or 4 stones; the player who cannot move loses (no stones left on their…
- Problem: On the 4-cycle C4 (vertices 0-1-2-3-0), a walker starts at 0 and takes exactly 3 steps, each to an adjacent vertex. How many…