Lesson overview · Free interview practice

Greedy Algorithms

Solve optimization problems using greedy strategies.

Topics in the full lesson
  • Concept and Use Cases
  • When to Use
  • Time and Space Complexity
  • Common Problems
  • Activity Selection Problem
  • Fractional Knapsack Problem
  • Prim’s Algorithm

Practice Problems

Work through a question before revealing its explanation. These questions and answers are free; Premium adds the full lesson walkthrough, examples and implementation detail.

Completion marks record your own progress, not an automatically checked result. The task type does not determine whether it is optional.

CodingMedium

Gas Station

Question

There are n gas stations along a circular route. The amount of gas at the i-th station is gas[i]; it costs cost[i] of gas to travel from station i to station i+1. You start with an empty tank at one station. Return the starting station's index if you can travel around the circuit once in the clockwise direction; otherwise return -1. If a solution exists, it is guaranteed to be unique.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

CodingMedium

Jump Game

Question

You are given an integer array nums where you are initially at index 0. Each nums[i] represents your maximum jump length from that position. Return true if you can reach the last index, otherwise false.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

CodingHard

Candy

Question

There are n children standing in a line, each with a rating in ratings. You must give each child at least 1 candy, and children with a higher rating than an adjacent child must receive more candies than that neighbor. Return the minimum number of candies you need.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

Explore the full Greedy Algorithms material

Premium includes the complete lessons and implementation references. Free practice questions remain available without a subscription.

All course tracks & premium content
From basics to advanced masterclasses
Built for JS/TS developers like you
Real-world tips & common pitfalls
Upgrade to Premium