LESSON 30 / 75·2 min readIntermediate

Greedy and binary search in JavaScript

Good to knowConcept lesson
01 Understand02 Recall03 Prepare

Common greedy questions

  • Jump Game
  • Assign Cookies
  • Activity Selection
  • Minimum Coins
JavaScript
function binarySearch(arr, target) {
  let left = 0;
  let right = arr.length - 1;

  while (left <= right) {
    const mid = Math.floor((left + right) / 2);
    if (arr[mid] === target) return mid;
    if (arr[mid] < target) left = mid + 1;
    else right = mid - 1;
  }

  return -1;
}

PAUSE & RECALL

Can you explain it without the notes?

Greedy and binary search in JavaScript

Use your own words. Explain an example and one limitation before checking the explanation.

Check your explanation

Common greedy questions

CONNECT IT TO YOUR INTERVIEW

Turn this concept into a clear answer.

Practice this concept
YOUR NEXT STEP

Keep connecting the ideas.

Next lesson: Sliding window and two pointers in JavaScript

Adapted from 16_DSA_JAVASCRIPT.md · “11. Greedy and binary search in JavaScript”. Examples are study fragments; framework behavior can depend on your version.