Data Structures & AlgorithmsHardTCS Digital · Infosys pattern

A 'leader' is an element strictly greater than every element to its right (the last element is always a leader). Scanning from the right, which leaders does [12, 30, 7, 18, 9, 11, 4] have, in left-to-right order?

  1. A. 30, 18, 11
  2. B. 12, 30, 18, 11, 4
  3. C. 30, 11, 4
  4. D. 30, 18, 11, 4

Correct answer: D. 30, 18, 11, 4

From the right, keep the running maximum: 4 (leader), 11 > 4 (leader), 9 no, 18 > 11 (leader), 7 no, 30 > 18 (leader), 12 no. One O(n) pass. Leaving out 4 forgets that the last element always qualifies; 12 is not a leader because 30 lies to its right.

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

Exams that ask this topic