Here is the interview question prompt, presented for reference.
A circular array
is one where the next
element of the last element is the first element.
You know how standard arrays look. Instead of [1, 2, 3, 4]
, imagine the following, where after index 7
, we'd move back to index 0
.
Can you write a method nextLargerNumber(nums: array)
to print the next immediate larger number for every element in the array?
Note: for any element within the circular array, the next immediate larger number could be found circularly-- past the end and before it. If there is no number greater, return -1
.
Take the following example, with an analysis for each index:
nextLargerNumber([3, 1, 3, 4])
// [4, 3, 4, -1]
// 3's next greater is 4
// 1's next greater is 3
// 3's next greater is 4 again
// 4 will look to the beginning, but find nothing, so -1
100000
-1000000000
and 1000000000
O(n)
O(n)
You can see the full challenge with visuals at this link.
Challenges • Asked almost 7 years ago by Jake from AlgoDaily
This is the main discussion thread generated for Next Greater Element in a Circular Array.