Prepin
Log in

Quickly identify the next largest element in an array.

Approach / Explanation

This question asks you to identify and return the next greater element for each item in an array. For example, in the array [4,5,2,25], the next greater element for 2 is 5, and for 5, it is 25. Since there is no element larger than 25 in the array, it will return -1 for 25. Successfully addressing this problem requires a solid grasp of array mechanics and the application of algorithmic reasoning. A suggested method to tackle this problem involves using a stack data structure. The core concept is to push an element onto the stack, and for each following element, if it exceeds the top element of the stack, it indicates that this new element is the next greater one for all the elements in the stack up to that point. Therefore, we continue to pop elements from the stack until we encounter an element that is greater in the stack or until the stack is empty. We then push this new element onto the stack.

Suggested Answer

Below is an example written in Python: def nextGreaterElement ( array ) : stack = [ ] result = [ - 1 ] * len ( array ) for i in range ( len ( array ) ) : while stack and array [ i ] > array [ stack [ - 1 ] ] : result [ stack . pop ( ) ] = array [ i ] stack . append ( i ) return result This solution involves creating a stack and an array filled with -1s to store the final results. We loop through the provided array, and for each element, we check if the stack is non-empty and whether the current element is greater than the element at the top of the stack. As long as this condition holds true, it indicates that the current element is the next greater element for the element at the index stack[-1]. Therefore, we pop the top index from the stack and update the result at this index to the current element. Regardless of the condition, we push the current index onto the stack. Ultimately, the resulting array will contain the next greater elements for all elements in the provided array.

Alternative Answer

Here’s another example written in Java: public void nextGreaterElement ( int [ ] array ) { Stack < Integer > stack = new Stack < > ( ) ; int [ ] result = new int [ array . length ] ; for ( int i = array . length - 1 ; i >= 0 ; i -- ) { while ( ! stack . isEmpty ( ) && stack . peek ( ) <= array [ i ] ) { stack . pop ( ) ; } if ( ! stack . isEmpty ( ) ) { result [ i ] = stack . peek ( ) ; } else { result [ i ] = - 1 ; } stack . push ( array [ i ] ) ; } } This solution employs a similar strategy but iterates through the array in reverse. For each element, we continue to pop from the stack until we locate an element that is greater than the current item or until the stack is empty. If the stack is not empty, it indicates that the top item in the stack is the next greater element for the current item in the array. Conversely, if the stack is empty, it signifies that there is no next greater element. We push the current item in the array onto the stack regardless. This process continues for the remaining elements in the array.

Question Details

Difficulty & Category

Medium
algorithms

Companies

Meta (Facebook)

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.