All puzzles › Computer Science

Computer Science Practice Problems

32 puzzles (9 easy, 15 medium, 8 hard), 10 free, in Python, JavaScript and C++.

Ciphers, sorting, binary and other CS building blocks.

The puzzles, easiest first

  1. Store the Surprise, Not the Time (Easy, free): A monitoring system records a reading every ten seconds, so its timestamps are large numbers that are almost perfectly predictable.
  2. Binary to Decimal (Easy): Given an array of bits, return the number it represents.
  3. Caesar Cipher (Easy): Given a lowercase string and a number k, shift every letter forward k places in the alphabet, wrapping from z back to a.
  4. Count Set Bits (Easy): Given a number, return how many 1 bits it has in binary.
  5. Count Vowels (Easy): Given a string, return how many vowels it has (a, e, i, o, u).
  6. Decimal to Binary (Easy): Given a number that's 0 or more, return its binary digits as an array.
  7. Hex to Decimal (Easy): Given a hex string using 0-9 and a-f, return its decimal value.
  8. Linear Search (Easy): Given an array and a target, return the index of the first match, or -1 if it isn't there.
  9. Reverse a String (Easy): Given a string, return it backwards.
  10. Copy From What You Already Wrote (Medium, free): LZ77, the idea inside nearly every compressor, replaces repeated data with a pointer backward: go back this far and copy this many.
  11. Counting Without a Slot per Item (Medium, free): To count how often each item appears in a huge stream, a hash map needs an entry per item.
  12. Cut Where the Data Says (Medium, free): A backup tool splits files into chunks and stores each distinct chunk once.
  13. Shuffle It So It Compresses (Medium, free): The Burrows-Wheeler transform rearranges a string so that equal letters end up side by side, which makes it far easier to compress, and the rearrangement can be undone exactly.
  14. Similar Sets, Tiny Fingerprints (Medium, free): Comparing two huge sets item by item is slow.
  15. Whoever Is Left Standing (Medium, free): You want the items that make up more than one k-th of an endless stream, and there is no room to count everything.
  16. Bubble Sort (Medium): Given an array of numbers, sort it with bubble sort.
  17. FizzBuzz (Medium): Given n, return the numbers 1 to n as strings, but use "Fizz" for multiples of 3, "Buzz" for multiples of 5, and "FizzBuzz" for multiples of both.
  18. Hamming Distance (Medium): Given two bit arrays of the same length, return how many positions differ.
  19. Insertion Sort (Medium): Given an array, sort it with insertion sort, the way you'd sort cards in your hand.
  20. Isomorphic Strings (Medium): Given two strings, return true if you can turn one into the other by swapping each letter for another letter, the same way every time.
  21. Kth Largest Element (Medium): Given an array and k, return the kth largest value, where k = 1 means the biggest.
  22. Reverse Bits (Byte) (Medium): Given a number from 0 to 255, reverse the order of its 8 bits and return the result.
  23. Run-Length Encoding (Medium): Given a string, compress it by writing each run of repeated letters as the letter followed by how many times it repeats.
  24. Selection Sort (Medium): Given an array, sort it with selection sort.
  25. A Running Total You Can Edit (Hard, free): A table of running totals answers any range sum at once, but changing one number means rebuilding it.
  26. Count by the Longest Run of Zeros (Hard, free): HyperLogLog counts distinct items using a few bytes, by noticing how rare a hash that starts with many zeros is.
  27. Kick Out the Occupant (Hard, free): Cuckoo hashing gives every key two homes, one in each of two tables, so a lookup checks two slots and is done.
  28. Longest Common Prefix (Hard): Given a list of strings, return the longest prefix they all share, or "" if they share none.
  29. Longest Increasing Subsequence (Hard): Given an array, return the length of the longest strictly increasing subsequence.
  30. Merge Sort (Hard): Given an array, sort it with merge sort.
  31. Search Rotated Array (Hard): Given a sorted array that's been rotated at some unknown point, like [4, 5, 6, 7, 0, 1, 2], return the index of the target, or -1.
  32. Word Break (Hard): Given a string and a list of dictionary words, return true if the string can be split completely into dictionary words.

Practice another pattern

Or browse by subject

Browse all 676 puzzles