Data Structures & AlgorithmsModerateTCS NQT Digital · Product Company SDE
Problem: What is the maximum sum of a subsequence of [4, −1, 2, 1] with no two adjacent elements chosen (the empty selection is not allowed)?
- A. 5
- B. 7
- C. 4
- D. 6
Correct answer: D. 6
Choosing 4 and 2 (indices 0 and 2) gives 6, the best non-adjacent pair. 4 + 1 gives 5, adding all positives ignores adjacency (7), and stopping after the first element gives 4.
More Coding Interview Problems questions
- Problem: The string "ABCABCABC" is read and each letter is replaced by its position in the alphabet (A=1, B=2, C=3). What is the sum of…
- Problem: From the list [12, 18, 24, 30, 36], what is the number of distinct prime divisors of the product of all five numbers?
- Problem: The array [7, 1, 5, 3, 6, 4] is processed to find the maximum profit from one buy and one later sell (0 if none). What is the…
- Problem: A grid of 3×3 cells is numbered 1 to 9 row by row. Starting at 1 (top-left), the robot moves right or down. The number of paths…
- Problem: A list of intervals is [1,5], [2,3], [4,8], [7,9], [10,11]. Merge all overlapping intervals (touching at an endpoint counts as…
- Problem: The string "aabbbcccc" is encoded by writing each letter followed by its count, e.g. "a2b3c4". What is the length of the encoded…