Data Structures & AlgorithmsModerateTCS NQT · Accenture
Problem: The array [1, 4, 3, 2] (with even length) is split into two-element pairs in any way, and the minimum of each pair is added up. What is the maximum possible total?
- A. 5
- B. 3
- C. 4
- D. 6
Correct answer: C. 4
Pairing sorted neighbours maximises the sum of mins, since each minimum is then as large as possible. Pairing the two largest together wastes their size.
More Coding Interview Problems questions
- Problem: A matrix stores the number of paths in a 3×3 grid where each cell can be entered from the left or from above, and blocked cells…
- Problem: The string "racecar" has length 7. How many of its substrings are palindromes with length at least 3? Count index ranges.
- Problem: Count the pairs (i, j) with i < j in the array [1, 5, 2, 8, 3, 5] whose absolute difference is at most 2.
- Problem: A dice-throwing game: starting at 0, each throw adds a value from 1 to 6, and the player stops as soon as the total is at least…
- Problem: Dijkstra is run on a directed graph with arcs 0→1 (5), 0→2 (2), 2→1 (1), 1→3 (2), 2→3 (7), 3→4 (1), 2→4 (9). Each vertex is…
- Problem: A directed graph on vertices 0–4 has arcs 0→1, 1→2, 2→0, 2→3, 3→4. How many distinct simple directed cycles does it contain?