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)?

  1. A. 5
  2. B. 7
  3. C. 4
  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

Exams that ask this topic