Workspace Assign Cookies
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Assign Cookies visualizer

Learn Assign Cookies with a step-by-step Greedy & Intervals visualizer, JavaScript source code and a worked example. Practice scheduling and resource allocation.

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 / 3Execution step
Reference example · JavaScript
Inspect reference input
{
  "g": [
    1,
    2,
    3
  ],
  "s": [
    1,
    1
  ]
}

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = ({
g,s:cookie
},s)=>{
g.sort((a,b)=>a-b);
cookie.sort((a,b)=>a-b);
let i=0;
for(let j=0;
j<cookie.length;
j++){
if(i<g.length&&cookie[j]>=g[i])i++;
s("Assign smallest adequate cookie",{
Children:g,Cookies:cookie,Served:[i]
},[j])
}
return i
};

Execution canvas

Active Changed
STEP 1

Assign smallest adequate cookie

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

Children3 items
Cookies2 items
Served1 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Greedy & Intervals · Scheduling and resource allocation

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

{
  "g": [
    1,
    2,
    3
  ],
  "s": [
    1,
    1
  ]
}

Execution steps

  1. Assign smallest adequate cookie

Computed output

1
JavaScript reference implementation
const solve = ({
  g,s:cookie
},s)=>{
  g.sort((a,b)=>a-b);
  cookie.sort((a,b)=>a-b);
  let i=0;
  for(let j=0;
  j<cookie.length;
  j++){
    if(i<g.length&&cookie[j]>=g[i])i++;
    s("Assign smallest adequate cookie",{
      Children:g,Cookies:cookie,Served:[i]
    },[j])
  }
  return i
};

Continue learning

Practice related DSA questions in the same topic.