Back to questionsDSA Visualizers: 388 Coding Questions Online Stock Span visualizer Learn Online Stock Span with a step-by-step Stacks & Queues visualizer, JavaScript source code and a worked example. Practice monotonic structures.
INTERACTIVE EXECUTION STUDIOLEARN BY DOING Predict. Execute. Understand. Follow the source code, inspect live state changes, and predict the next operation before revealing it.
1 / 8 Execution step
Prediction modePanel width Reference example · JavaScript
Inspect reference input [
100,
80,
60,
70,
60,
75,
85
] Code Visuals
JavaScript reference implementation · highlighted trace statement
1 const solve = (a,s)=>{
2 let st=[],o=[];
3 for (let i=0 ;
4 i<a.length;
5 i++){
6 while (st.length&&a[st.at(-1 )]<=a[i])st.pop();
7 o.push(i-(st.at(-1 )??-1 ));
8 st.push(i);
9 s("Find previous greater price",{
10 Prices:a,Stack:st,Span:o
11 },[i])
12 }
13 return o
14 };
Execution canvas Active ChangedSTEP 1 Find previous greater price Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.
Worked example and algorithm walkthrough Topic: Stacks & Queues · Monotonic structures
Follow the computed execution on the reference input. Advance one step at a time, inspect the changing values and compare your prediction with the final output.
Example input [
100,
80,
60,
70,
60,
75,
85
] Execution steps Find previous greater price Computed output [
1,
1,
1,
2,
1,
4,
6
] JavaScript reference implementation const solve = (a,s)=>{
let st=[],o=[];
for(let i=0;
i<a.length;
i++){
while(st.length&&a[st.at(-1)]<=a[i])st.pop();
o.push(i-(st.at(-1)??-1));
st.push(i);
s("Find previous greater price",{
Prices:a,Stack:st,Span:o
},[i])
}
return o
};Continue learning Practice related DSA questions in the same topic.