All guides

Programmer interview questions, with runnable solutions

At programmer interviews, beyond theory questions, you are almost always asked to write code: small problems that test whether you can reason, not whether you memorised. Here are the most common classics, with explained solutions you can run right on this page.

1. FizzBuzz

Print 1 to 15; for multiples of 3 write "Fizz", of 5 "Buzz", of both "FizzBuzz".

The most famous filter: trivial if you can program, revealing if you can't.

for n in range(1, 16):
    if n % 3 == 0 and n % 5 == 0:
        print("FizzBuzz")
    elif n % 3 == 0:
        print("Fizz")
    elif n % 5 == 0:
        print("Buzz")
    else:
        print(n)

Order matters: the "both" condition must come first, otherwise it is never reached.

2. Reverse a string

Given a word, print it backwards.

word = "programming"
print(word[::-1])

[::-1] walks the string backwards. In an interview, being able to explain a manual loop version too makes a good impression.

3. Count the vowels

Count how many vowels are in a sentence.

sentence = "hello world"
vowels = 0

for letter in sentence:
    if letter in "aeiou":
        vowels += 1

print("Vowels:", vowels)

letter in "aeiou" is true when the letter is a vowel.

4. Is the number a palindrome?

A number is a palindrome if it reads the same left to right and right to left (e.g. 121).

function palindrome(n) {
  const s = String(n);
  return s === s.split("").reverse().join("");
}

console.log(palindrome(121));
console.log(palindrome(123));

Turn the number into a string, reverse it and compare with the original.

5. Find the max without using max

Find the biggest value in a list, without built-in helpers.

let numbers = [4, 9, 1, 7, 12, 3];
let biggest = numbers[0];

for (let n of numbers) {
  if (n > biggest) {
    biggest = n;
  }
}

console.log("Max: " + biggest);

Many "hard" questions are variations of this: scan, compare, keep the best.

How to really prepare

Coding questions are passed through practice, not theory: you must have solved many beforehand. The Algorithms path and the language paths on LevelUpCode drill exactly this kind of problem, with instant in-browser execution. Just want to try the solutions above? Open them in the playground.

Learn by doing, not just reading

Zero-to-advanced paths with exercises you write and run in the browser. The first chapters are free.

Programmer interview questions, with runnable solutions — LevelUpCode