Data Structures & AlgorithmsHardGATE CS · TCS NQT

A routine builds a list t: for each x in a, it appends x to t only if x is larger than the last element of t (otherwise it skips x), and it returns len(t). For the array [5, 1, 2, 3, 4], what value does the routine return?

  1. A. 4
  2. B. 2
  3. C. 1
  4. D. 5

Correct answer: C. 1

The routine commits to the first element 5 and never reconsiders it, so it returns 1. The true LIS of [5, 1, 2, 3, 4] is 1, 2, 3, 4 with length 4. Greedy appending fails because it can lock out a better start.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic