All puzzles › Arrays and Strings

Arrays and Strings Practice Problems

68 puzzles (37 easy, 28 medium, 3 hard), 23 free, in Python, JavaScript and C++.

Arrays and strings are where almost every interview starts, because they are the simplest thing that can hold more than one value and nearly every harder structure is built on one. The problems here are about indexing without stepping off the end, changing a sequence in place, walking a grid, and keeping a running total so you never add the same numbers twice.

How to spot a arrays and strings problem

Reach for these techniques when the input is a plain sequence and the question is about positions: the element before or after, a range from i to j, a row and a column. If you catch yourself re-adding a stretch you already added, a prefix sum is usually the fix.

New to it? Read the lesson first: Arrays.

The puzzles, easiest first

  1. A Floor That Moves (Easy, free): Min-p sampling throws away unlikely tokens, but the cutoff moves with how sure the model is.
  2. Already Paid For (Easy, free): A model server can skip work for any prompt that starts the same way as one it has already processed.
  3. How Far Back It Can See (Easy, free): Each layer of a convolutional network sees a little further than the one below it.
  4. Shades From Black and White (Easy, free): A screen that can only show black or white can still fake gray, by lighting some pixels and not others.
  5. 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.
  6. Two Bits Finish the Job (Easy, free): Teleporting a qubit takes a shared entangled pair and two ordinary bits.
  7. Two Replicas, One Count (Easy, free): Two replicas count likes while disconnected from each other, and the counts must agree when they reconnect, without losing or double counting anything.
  8. Two Thresholds Stop the Flicker (Easy, free): A thermostat with one threshold would click on and off many times a minute as the temperature hovered around it, and wear out the heater.
  9. Which Version Can I See (Easy, free): A database with snapshot isolation never overwrites a row.
  10. Acrostic Initials (Easy): Given the lines of a poem, return the first letter of each line, uppercased and joined together.
  11. Apply Dropout Mask (Easy): Given a list of activations and a mask of 0s and 1s, return the activations with every masked-out spot set to 0.
  12. Argmax (Predicted Class) (Easy): Given a list of numbers, return the INDEX of the biggest one, not the value.
  13. As Even As It Gets (Easy): Given a total number of lines and a number of pieces, return the size of each piece, as even as possible.
  14. 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.
  15. Count Land Cells (Easy): Given a grid of "1"s and "0"s, return how many cells are "1".
  16. Count Vowels (Easy): Given a string, return how many vowels it has (a, e, i, o, u).
  17. Count Words (Easy): Given a line of text, return how many words it has.
  18. Do They Rhyme? (Easy): Given two words, return true if they rhyme, which here means they end in the same two letters.
  19. Dot Product (Easy): Given two arrays of the same length, return their dot product.
  20. Embedding Lookup (Easy): Given an embedding table with one row per token, and a list of token IDs, return the row for each ID, in order.
  21. Flatten Feature Map (Easy): Given a 2D grid, return all its values in one flat array, row by row.
  22. Flattening the Grid (Easy): Given a grid's rows and columns, a cell's row r and column c, and whether it's stored row-major, return how many spots from the start that cell is in memory.
  23. Hex to Decimal (Easy): Given a hex string using 0-9 and a-f, return its decimal value.
  24. Leaky ReLU (Easy): Given a list of numbers, apply Leaky ReLU to each one.
  25. Lowest Note (Root) (Easy): Given the notes of a chord, return the lowest one.
  26. Max Consecutive Ones (Easy): Given an array of 1s and 0s, return the length of the longest run of 1s in a row.
  27. Missing Number (Easy): Given n different numbers from 0 to n, with exactly one missing, return the missing one.
  28. One-Hot Encode (Easy): Given a class index and the number of classes, return an array of zeros with a single 1 at that index.
  29. Plus One (Easy): Given an array of digits that spells out a number, add one and return the new digits.
  30. ReLU Activation (Easy): Given a list of numbers, apply ReLU to each one and return the results.
  31. Reverse a String (Easy): Given a string, return it backwards.
  32. Running Sum (Easy): Given an array, return a new array where each spot holds the total of everything up to and including it.
  33. Tanh Activation (Easy): Given a list of numbers, apply tanh to each one, rounded to 4 decimals.
  34. The Steady Clock (Easy): Every game loop draws frames as fast as the screen allows, but physics has to advance in a fixed timestep or it behaves differently on fast and slow machines.
  35. Tokenize Text (Easy): Given a sentence, return its words in lowercase.
  36. Transpose Down (Easy): Given a list of MIDI notes and a number of semitones, return every note lowered by that much.
  37. Which End First? (Easy): Given the bytes of an integer in memory order and whether the machine is little endian, return the integer.
  38. Allocate by Moving One Number (Medium, free): The fastest allocator there is keeps a single number: where the free space begins.
  39. Any Rectangle in Four Lookups (Medium, free): Adding up the pixels in a rectangle normally means visiting every one.
  40. Cross Off What Cannot Work (Medium, free): Before a solver guesses, it removes values that could never be part of an answer.
  41. Cut the Picture Into Words (Medium, free): A vision transformer reads an image the way a language model reads a sentence, so the image first has to become a sequence.
  42. Double, Then Creep (Medium, free): TCP does not know how fast the network is, so it probes.
  43. Find the Flip Without Looking (Medium, free): Measuring a qubit destroys what it holds, so a quantum code never looks at its data.
  44. Fold In the Adapter (Medium, free): LoRA fine-tunes a model without touching its weights: it trains two thin matrices, b and a, whose product is the change.
  45. Hold Back the Tail (Medium, free): A chat interface shows text as it arrives, but must never show the stop sequence, and the stop sequence can arrive split across chunks.
  46. One Question, Best Asked (Medium, free): A decision tree is built one split at a time, and each split is this puzzle.
  47. Pass the Error Along (Medium, free): Turning gray into black and white always gets each pixel a little wrong.
  48. 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.
  49. Steer Toward the Target (Medium, free): A PID controller is the loop inside cruise control, drones, ovens and 3D printers.
  50. Step by the Safe Distance (Medium, free): A ray starts at x = 0 and travels right along the x axis.
  51. Trust the Sensor, but Not Too Much (Medium, free): A Kalman filter turns noisy readings into a steady estimate, and it is how a phone smooths its GPS position.
  52. Causal Attention Mask (Medium): Given n, return an n by n grid where row i has a 1 in every column up to and including i, and 0 after.
  53. Count the Misses (Medium): Given the pages a program uses, in order, and how many frames of memory it has, return how many page faults happen under least recently used (LRU) replacement.
  54. Hit or Miss (Medium): Given the byte addresses a program reads, in order, a cache's block size, and its number of lines, return how many reads hit in a direct-mapped cache that starts empty.
  55. How Far Does the Head Travel (Medium): Given disk requests by cylinder, where the head is now, and the highest cylinder, return how far the head travels.
  56. Min-Max Scaling (Medium): Given a list of numbers, scale them into the range 0 to 1, rounded to 3 decimals.
  57. Payback Period (Medium): Given what an investment cost and a list of monthly cash returns, return the first month (counting from 1) where the running total covers the cost.
  58. Product of Array Except Self (Medium): Given an array, return a new array where each spot holds the product of every other number, without using division.
  59. Redaction Count (Medium): Given a list of words and a target word, return how many times the target shows up.
  60. Rotate Array (Medium): Given an array and k, shift every item k places to the right, with items that fall off the end wrapping around to the front.
  61. 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.
  62. Standing on Solid Ground (Medium): A tilemap stores a level as a grid, which turns collision into arithmetic.
  63. Syllable Estimator (Medium): Given a word, estimate its syllables by counting groups of vowels in a row.
  64. The Hidden Bytes (Medium): Given the sizes of a C struct's fields, in order, return the struct's total size in bytes.
  65. The Perspective Divide (Medium): Given a point in clip space as [x, y, z, w], return [x / w, y / w, z / w], rounded to 4 decimals.
  66. 2D Convolution (Hard): Given an image and a smaller kernel, both as 2D grids, slide the kernel over every spot where it fully fits and return the grid of weighted sums, rounded to 3 decimals.
  67. Longest Common Prefix (Hard): Given a list of strings, return the longest prefix they all share, or "" if they share none.
  68. Spiral Matrix Order (Hard): Given a matrix, return its values in spiral order: across the top, down the right side, back along the bottom, up the left side, then inward.

Practice another pattern

Or browse by subject

Browse all 676 puzzles