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?
- A. 30, 18, 11
- B. 12, 30, 18, 11, 4
- C. 30, 11, 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- The words stop, pots, opts, post, tops, spot, pot, top are grouped so that anagrams share a group (key = the sorted letters). How many…
- A stack is built from two queues with a costly push: enqueue x into the empty queue Q2, move every element of Q1 into Q2 one by one, then…
- With at most k = 2 character replacements, what is the length of the longest substring of 'AABCABBB' that can be turned into a single…
- What is the length of the longest contiguous subarray of [0, 1, 1, 0, 1, 1, 1, 0, 0, 0] with an equal number of 0s and 1s? (Map 0 to −1…
- What is the maximum sum of a non-empty contiguous subarray of the CIRCULAR array [5, −8, 3, 4, −2, 6, −9, 7] (a subarray may wrap from the…
- What is the maximum product of a non-empty contiguous subarray of [2, −3, 0, −2, −4, 3, −1]?