Coding interview solutions

Step-by-step solutions to 45 common coding interview problems, each with the optimal approach and its time & space complexity, explained in Python and JavaScript. Prefer to try first? Practice these problems online.

Two Sum solution

Two Sum: finding two numbers in an array that add up to a given target number

easy
arrays
hashmap

Merge Intervals solution

Given a collection of intervals, merge any overlapping intervals.

medium
arrays
sorting

Reverse a String solution

reverse a string interview question

easy
strings

Longest Common Prefix solution

Find the longest common prefix string amongst an array of strings.

easy
strings

Palindrome String solution

Palindrome String: Free. No login required. Python technical interview question

easy
strings
two-pointers

Roman to Integer solution

Given a Roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.

easy
strings
math

Letter Combinations of a Phone Number solution

Write a function to generate all possible combinations of letters that can be formed from a given string of digits representing a phone number.

medium
strings
backtracking

Valid Parentheses solution

valid parentheses coding question

easy
strings
stack

Find the Index of the First Occurrence in a String solution

Learn how to find the index of the first occurrence of a substring in a string in this coding interview problem.

easy
strings
two-pointers

Length of Last Word solution

Find the length of the last word in a given string.

easy
strings

Spiral Matrix solution

given number n return matrix n by n with numbers in spiral order

medium
arrays
matrix

FizzBuzz solution

Classic FizzBuzz: return a list from 1 to n with Fizz/Buzz substitutions.

easy
strings
math

Contains Duplicate solution

Return true if any value appears at least twice in the array.

easy
arrays
hashmap

Maximum Subarray solution

Find the contiguous subarray with the largest sum and return that sum.

medium
arrays
dynamic-programming

Fibonacci Number solution

Return the nth Fibonacci number (0-indexed).

easy
math
dynamic-programming

Factorial solution

Compute n! (the factorial of a non-negative integer n).

easy
math
recursion

Move Zeroes solution

Move all zeroes to the end while keeping the order of non-zero elements.

easy
arrays
two-pointers

Single Number solution

Every element appears twice except one. Find the single one.

easy
arrays
bit-manipulation

Best Time to Buy and Sell Stock solution

Maximize profit from a single buy/sell of a stock given daily prices.

easy
arrays
dynamic-programming

Valid Anagram solution

Determine whether one string is an anagram of another.

easy
strings
hashmap

Binary Search solution

Return the index of target in a sorted array, or -1 if not present.

easy
arrays
binary-search

Count Vowels solution

Count the number of vowels in a string.

easy
strings

Climbing Stairs solution

Count distinct ways to climb n stairs taking 1 or 2 steps at a time.

easy
math
dynamic-programming

Product of Array Except Self solution

Return an array where each element is the product of all others.

medium
arrays
prefix-sum

Trapping Rain Water solution

Given an elevation map, compute how much water it can trap after raining.

hard
arrays
two-pointers

Edit Distance solution

Find the minimum number of edit operations to convert one word into another.

hard
strings
dynamic-programming

Median of Two Sorted Arrays solution

Find the median of two sorted arrays in logarithmic time.

hard
arrays
binary-search

Longest Valid Parentheses solution

Find the length of the longest valid well-formed parentheses substring.

hard
strings
stack

Palindrome Number solution

Determine whether an integer reads the same backward as forward.

easy
math

Plus One solution

Add one to a number represented as an array of digits.

easy
arrays
math

Majority Element solution

Find the element that appears more than half the time in an array.

easy
arrays
hashmap

Is Subsequence solution

Check whether one string is a subsequence of another.

easy
strings
two-pointers

First Unique Character in a String solution

Find the index of the first non-repeating character in a string.

easy
strings
hashmap

Search Insert Position solution

Return the index where a target should be inserted into a sorted array.

easy
arrays
binary-search

House Robber solution

Maximize the loot from non-adjacent houses along a street.

medium
arrays
dynamic-programming

Jump Game solution

Decide whether you can reach the last index of an array of jump lengths.

medium
arrays
greedy

Coin Change solution

Find the fewest coins needed to make up a given amount.

medium
arrays
dynamic-programming

Longest Substring Without Repeating Characters solution

Find the length of the longest substring without repeating characters.

medium
strings
sliding-window

Container With Most Water solution

Find two lines that together with the x-axis hold the most water.

medium
arrays
two-pointers

Number of Islands solution

Count the number of islands in a grid of land and water cells.

medium
matrix
graph

Word Break solution

Determine whether a string can be segmented into words from a dictionary.

hard
strings
dynamic-programming

Longest Increasing Subsequence solution

Find the length of the longest strictly increasing subsequence.

hard
arrays
dynamic-programming

Largest Rectangle in Histogram solution

Find the area of the largest rectangle that fits under a histogram.

hard
arrays
stack

Sliding Window Maximum solution

Return the maximum of every contiguous window of size k.

hard
arrays
sliding-window

Jump Game II solution

Find the minimum number of jumps needed to reach the last index.

hard
arrays
greedy

FAQ

See also: