Workspace Remove K Digits
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Remove K Digits visualizer

Learn Remove K Digits with a step-by-step Stacks & Queues visualizer, JavaScript source code and a worked example. Practice monotonic structures.

INTERACTIVE EXECUTION STUDIO
LEARN BY DOING

Predict. Execute. Understand.

Follow the source code, inspect live state changes, and predict the next operation before revealing it.

1 / 8Execution step
Reference example · JavaScript
Inspect reference input
{
  "a": "1432219",
  "k": 3
}

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = ({
a,k
},s)=>{
let st=[];
for(let x of a){
while(k&&st.length&&st.at(-1)>x){
st.pop();
k--
}
st.push(x);
s("Remove larger preceding digits",{
Stack:st,Remaining:[k]
})
}
if(k)st.splice(st.length-k,k);
return st.join("").replace(/^0+/,"")||"0"
};

Execution canvas

Active Changed
STEP 1

Remove larger preceding digits

Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.

Stack1 items
Remaining1 items
Keyboard: Space to play · Left / Right to stepExplore next problem

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

{
  "a": "1432219",
  "k": 3
}

Execution steps

  1. Remove larger preceding digits

Computed output

"1219"
JavaScript reference implementation
const solve = ({
  a,k
},s)=>{
  let st=[];
  for(let x of a){
    while(k&&st.length&&st.at(-1)>x){
      st.pop();
      k--
    }
    st.push(x);
    s("Remove larger preceding digits",{
      Stack:st,Remaining:[k]
    })
  }
  if(k)st.splice(st.length-k,k);
  return st.join("").replace(/^0+/,"")||"0"
};

Continue learning

Practice related DSA questions in the same topic.