Algorithm Race — Algorithms

Race to pick the fastest algorithm: 10 levels of time and space analysis for the optimal solution. An algorithm game for interview prep.

What you learn in this game

When choosing an algorithm, go for the most efficient approach. Not every problem needs sorting: sometimes a single pass is enough.

A Hash Map gives you O(1) lookup. Instead of an O(n²) nested loop, we can store data in a Map and check whether an element exists in constant time.

Sliding Window is a technique where we keep a fixed-size "window" that slides along the array. Instead of recomputing every time, we only update the difference.

Not all sorting algorithms are equal. Insertion Sort has an O(n) best case on nearly sorted data, because the inner loop barely runs. "Fancier" algorithms like Merge Sort don't take advantage of existing order at all.

A Hash Set gives you has() and add() in O(1). That suits "have I seen this before?" problems, which then need neither sorting nor a nested loop.

What the levels ask

  1. Pick the best approach for finding the largest value in an array.
  2. Pick the fastest way to check whether an array is already sorted.
  3. Find two numbers in the array that add up to the target.
  4. This array is bigger this time. Pick an approach that scales.
  5. Find the largest sum of k consecutive elements.
  6. Calculate the moving average of k elements.
  7. This array is nearly sorted. Pick the best sorting algorithm.
  8. Server log timestamps got slightly swapped. Put them back in order.
  9. Check whether the array has any duplicate elements.
  10. Find the shortest path in an unweighted graph.