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
Merge Intervals solution
Given a collection of intervals, merge any overlapping intervals.
Reverse a String solution
reverse a string interview question
Longest Common Prefix solution
Find the longest common prefix string amongst an array of strings.
Palindrome String solution
Palindrome String: Free. No login required. Python technical interview question
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.
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.
Valid Parentheses solution
valid parentheses coding question
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.
Length of Last Word solution
Find the length of the last word in a given string.
Spiral Matrix solution
given number n return matrix n by n with numbers in spiral order
FizzBuzz solution
Classic FizzBuzz: return a list from 1 to n with Fizz/Buzz substitutions.
Contains Duplicate solution
Return true if any value appears at least twice in the array.
Maximum Subarray solution
Find the contiguous subarray with the largest sum and return that sum.
Fibonacci Number solution
Return the nth Fibonacci number (0-indexed).
Factorial solution
Compute n! (the factorial of a non-negative integer n).
Move Zeroes solution
Move all zeroes to the end while keeping the order of non-zero elements.
Single Number solution
Every element appears twice except one. Find the single one.
Best Time to Buy and Sell Stock solution
Maximize profit from a single buy/sell of a stock given daily prices.
Valid Anagram solution
Determine whether one string is an anagram of another.
Binary Search solution
Return the index of target in a sorted array, or -1 if not present.
Count Vowels solution
Count the number of vowels in a string.
Climbing Stairs solution
Count distinct ways to climb n stairs taking 1 or 2 steps at a time.
Product of Array Except Self solution
Return an array where each element is the product of all others.
Trapping Rain Water solution
Given an elevation map, compute how much water it can trap after raining.
Edit Distance solution
Find the minimum number of edit operations to convert one word into another.
Median of Two Sorted Arrays solution
Find the median of two sorted arrays in logarithmic time.
Longest Valid Parentheses solution
Find the length of the longest valid well-formed parentheses substring.
Palindrome Number solution
Determine whether an integer reads the same backward as forward.
Plus One solution
Add one to a number represented as an array of digits.
Majority Element solution
Find the element that appears more than half the time in an array.
Is Subsequence solution
Check whether one string is a subsequence of another.
First Unique Character in a String solution
Find the index of the first non-repeating character in a string.
Search Insert Position solution
Return the index where a target should be inserted into a sorted array.
House Robber solution
Maximize the loot from non-adjacent houses along a street.
Jump Game solution
Decide whether you can reach the last index of an array of jump lengths.
Coin Change solution
Find the fewest coins needed to make up a given amount.
Longest Substring Without Repeating Characters solution
Find the length of the longest substring without repeating characters.
Container With Most Water solution
Find two lines that together with the x-axis hold the most water.
Number of Islands solution
Count the number of islands in a grid of land and water cells.
Word Break solution
Determine whether a string can be segmented into words from a dictionary.
Longest Increasing Subsequence solution
Find the length of the longest strictly increasing subsequence.
Largest Rectangle in Histogram solution
Find the area of the largest rectangle that fits under a histogram.
Sliding Window Maximum solution
Return the maximum of every contiguous window of size k.
Jump Game II solution
Find the minimum number of jumps needed to reach the last index.