~/boss / tower-view

Next greater element (monotonic stack)

The Tower Brawl

Who blocks whose view? Find every tower's next taller neighbour in one walk.

Loading the fight…

How to beat it

Walk the row left to right with a stack of towers still waiting for a taller one. A new tower arrives: taller than the top of the stack? It blocks that tower's view, so that's its answer: pop it and check the next one down. Not taller? It joins the stack and waits. Whoever still waits at the end never finds one: -1.

Now the real boss

Interviews ask for the code. Watch it run, then write it yourself, tested in Python, C++ or Java.

On your phone? Writing code is easier on a laptop: open bossfight.dev/problems/next-greater-element-i there. Sign in with your email to keep your progress on every device.

More bosses

esc