Data Structures & AlgorithmsHardAmazon · Microsoft coding round

For each element of the array [6, 2, 5, 4, 7, 3, 1, 8, 2] find the next greater element to its right (the value -1 if none exists). What is the sum of all these values?

  1. A. 48
  2. B. 50
  3. C. 32
  4. D. 44

Correct answer: A. 48

The next greater values are 7, 5, 7, 7, 8, 8, 8, -1, -1, which sum to 48. Counting 'none' as 0 gives 50, and scanning to the left instead gives 32.

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

Exams that ask this topic