{ }codelper

100 Coding Challenges for Beginners

Once you know the basic syntax, it is time to learn algorithms one type at a time. We picked 100 problems spread evenly across the types, easiest first, and you can solve them in Python, Java, C++ or another language you like.

100 problemsLevel 2–5Pick any of eight languages

1. Simulation and brute force #1–15

In these problems you turn the rules in the statement straight into code.
Being careful matters more than knowing algorithms.
When there are only a few cases, trying all of them (brute force) is the surest solution.
Estimate the number of cases first.

  1. A balance with unequal arms8P0345
  2. Fireflies sitting in a row8P0350
  3. Dice game: roll again on a 68P0264
  4. A tournament where winning tires you8P0180
  5. Tiles that flip when stepped on8P0352
  6. Narrowing down a PIN from worn keys8P0321
  7. What the progress bar left behind8P0271
  8. Broken Sensors and the Alarm8P0362
  9. Rewinding a work log to find the first row8P0331
  10. Laying Wires on an Endless Board8P0364
  11. Letter arrangements8P0100
  12. Cars at a traffic light8P0114
  13. A Tiny Heap Manager8P0365
  14. Round table seating rules8P0184
  15. Tile wall and corner numbers8P0399

2. Math: divisors, primes, number bases #16–30

Find the greatest common divisor with the Euclidean algorithm, and check many numbers for primes with the sieve of Eratosthenes.
Converting to another base means collecting the remainders in reverse order.
When the numbers get large, check the range of your data type first.

  1. Candies placed every k seats around a round table8P0313
  2. Count primes that stay prime reversed8P0258
  3. To binary8P0038
  4. Decoding hex color codes8P0416
  5. GCD and LCM8P0037
  6. Add fractions and simplify8P0094
  7. Counting when three lighthouses flash together8P0322
  8. Split into two primes8P0068
  9. From base a to base b8P0093
  10. Labels without the avoided digits8P0358
  11. Grouping recipes with the same ratio8P0280
  12. An elevator with two buttons8P0181
  13. Building a gear train8P0185
  14. Count numbers with k prime factors8P0113
  15. Primes in a smudged ledger8P0182

3. Sorting #31–41

Most of these are not about writing a sort yourself.
They are about choosing the order.
Learn to express several rules at once, such as score descending and then name ascending on a tie, with a comparison function or a key.

  1. Whose birthday comes next8P0376
  2. Word frequency ranking8P0034
  3. Live top three8P0109
  4. Score ranking8P0067
  5. Split by Stake and the Leftover Won8P0363
  6. Natural sort of file names8P0142
  7. Ranking stations by average temperature8P0195
  8. Ordering a warehouse stock ledger by cell8P0394
  9. Anagram groups8P0108
  10. Drawing a folder tree8P0248
  11. Sorting a table by column keys given in the input8P0308

4. Stacks, queues and heaps #42–56

A stack gives back the last thing you put in first (matching brackets).
A queue gives back the first thing you put in first (waiting in line).
A heap (priority queue) always gives you the smallest or the largest item quickly.
Learn what the ready-made data structures are called in your language.

  1. The pile of lunch trays8P0348
  2. Umbrella stand: first come, first served8P0351
  3. Cutting the longest rice cake first8P0379
  4. Balanced brackets8P0040
  5. Ticket line8P0071
  6. Lend the least-ridden bike first8P0294
  7. Coupling trains at the yard4P0207
  8. First greater number to the right8P0091
  9. A notepad with undo and redo8P0187
  10. Round-robin CPU8P0115
  11. Task queue and median3P0121
  12. A remainder calculator where adding comes first8P0186
  13. A ledger rolled back to named savepoints8P0305
  14. Counting two neighboring colors in a grouped bead design8P0400
  15. The kiln line where cracked pots cool and queue again8P0309

5. Binary search, two pointers, prefix sums #57–71

These are the techniques to use when a nested loop gets time limit exceeded.
Binary search throws away half of sorted data at each step.
Two pointers move the two ends or a range.
Prefix sums give you the sum of a range with a single subtraction.

  1. The busiest stretch of the bus8P0259
  2. Merge the logs of two entrances8P0260
  3. The books the librarian pulled out8P0375
  4. Songs played once in every k in a row8P0434
  5. Fast range sums8P0039
  6. Two numbers summing to S in a sorted list8P0069
  7. When is the room free?8P0240
  8. Height between the markers8P0327
  9. Fast rectangle sums on a grid8P0092
  10. Pairs within k8P0112
  11. Cutting the shortest piece from a necklace8P0155
  12. Cut ribbons evenly8P0070
  13. Backup disk size8P0111
  14. Transfers on a timetable where expresses overtake8P0304
  15. Fix by reference, find with lower_bound1P0125

6. Greedy and DP #72–86

Greedy picks whatever is best at that moment.
You should be able to explain why that choice is also right for the whole problem.
DP writes the answers to small problems in a table and reuses them for bigger ones.
Start by putting into words what one cell of the table means.

  1. Fewest coins8P0041
  2. Ways to climb stairs8P0042
  3. Most meetings in one room8P0090
  4. A rhythm game where combos build up8P0267
  5. Ways to make an amount with coins8P0088
  6. Longest increasing subsequence8P0089
  7. Packing a knapsack8P0116
  8. Longest common subsequence8P0117
  9. The cheapest way to fix a caption8P0153
  10. Choosing 3D printer orders that meet their deadlines8P0152
  11. The night watch that needs k people in every slot8P0310
  12. Ways the water can flow8P0154
  13. How many snacks to stock in the fridge8P0214
  14. Numbers that light S bars8P0279
  15. Taking marbles without copying8P0339

7. Graphs: DFS, BFS, topological sort #87–100

In these problems you follow things that are connected.
DFS goes down one path to the end, and BFS spreads out from the nearest places first.
If the question asks for the fewest steps, use BFS.
If you forget to mark places as visited, you go around the same ones forever.

  1. Stove warmth spreading through a classroom8P0378
  2. The seeker's walk in hide-and-seek8P0377
  3. Following the crossroad notes8P0380
  4. Ranks fixed by the clues alone8P0381
  5. The museum whose doors lock one by one8P0291
  6. Line numbers in a folded reply thread8P0292
  7. The person with the most friends in common8P0293
  8. A semester plan that keeps prerequisites8P0197
  9. Rebuild only the stale files8P0326
  10. The order of moves to measure with two jugs8P0210
  11. Exploring a cave by feel, picking up keys8P0190
  12. Friend groups8P0118
  13. Slack time of festival tasks8P0303
  14. A shortcut through up to K walls8P0119

Good to know

See also