Skip to content

Next greater element
one step at a time.

Keep a stack of numbers still waiting, falling from bottom to top; each new number pops the smaller ones and becomes their answer.

Free to use. Step forward and back at your pace, and share a link to the exact step you are stuck on.

What you actually watch.

The part a dry run on paper loses: the state, at every step.

Keep a stack of numbers still waiting, falling from bottom to top; each new number pops the smaller ones and becomes their answer. That sentence is easy to agree with and hard to hold in your head on step nine, which is where most people lose the thread.

So the visualiser keeps it in front of you: the stack growing and popping, with the item on top highlighted. The line of code being executed is highlighted as the picture changes, so the drawing and the code never drift apart. Step forward, step back, or let it play.

Put your own input in. Next greater element on a clean example teaches you the shape; next greater element on an empty input, a single element, or one with duplicates teaches you the thing an interviewer asks about.

How it runs, step by step.

The same order the visualiser moves in, so the list and the picture match.

  1. 1Read the input left to right.
  2. 2Push what is unfinished and still waiting for a partner.
  3. 3When the current item resolves the top of the stack, pop it and record the answer.
  4. 4What is left on the stack at the end is what never resolved.

Where people get this one wrong.

These are not made up. Each one is a switch in the tool: turn it on, run the same input, and watch the state go wrong.

  • Brute forceFor every number, scans everything to its right: up to n(n−1)/2 comparisons. Try "Falling".

Inputs worth trying.

The cases that teach something. Each one is a preset in the tool, with the reason it is there.

  • RisingEach number is answered by the very next one: one pop per push.
  • FallingNothing ever pops. Run brute force here: it compares every pair.
  • All equalEqual is not bigger, so nothing pops and every answer is −1.
  • One big pop6 answers three waiting numbers at once.

What it costs.

The two numbers an interviewer will ask for, and what they mean here.

Time
O(n)counted as real operations while it runs, not quoted from a table
Space
O(n)what the algorithm holds besides the input itself
Pattern
StackThe most recent unfinished thing is the one you need.

Once you can see it, go and use it.

Watching is the first ten minutes. The pattern sticks when you solve with it.

Everything in the app, in one place.

Practice, interviews and the tools, all free to start. Swipe to see the rest.

Free to start: 100 AI credits when you sign up and 100 every month. Start free in the app

About next greater element.

Open it in the app and the answers are easier to see than to read.

Open the visualiser
What does the next greater element visualiser show?

Every step of the algorithm on input you choose: the stack growing and popping, with the item on top highlighted, with the line of code being executed highlighted beside it. You can step forward, step back, or let it play. Spelled visualizer or visualiser, it is the same tool.

What is the time complexity of next greater element?

O(n) time and O(n) space. The visualiser counts the real operations as it runs, so you can watch the number grow instead of taking the formula on trust.

Can I use my own input?

Yes. Type your own array, string or graph and the algorithm runs on that, including the edge cases that break a solution in an interview.

Is it free?

Yes. The app is free to start, with 100 AI credits when you sign up and 100 every month, and watching an algorithm costs no credits.