Problem Bank

Master 123 essential DSA problems across 17 patterns, curated from FAANG interviews. Each problem includes detailed JavaScript solutions, complexity analysis, and company insights.

123
Total Problems
17
Patterns
FAANG
Interview Ready
100%
Free Access

Filters

Showing 123 of 123 problems

Given an integer array nums, return true if any value appears at least twice in the array.

Easy
Array & Hashing
Very High
Microsoft
Amazon
Apple
Airbnb
Time: O(n)
Space: O(n)

Given two strings s and t, return true if t is an anagram of s, and false otherwise.

Easy
Array & Hashing
Very High
Facebook
Amazon
Microsoft
Bloomberg
Time: O(n)
Space: O(1)

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

Easy
Array & Hashing
Very High
Google
Amazon
Microsoft
Facebook
+1 more
Time: O(n)
Space: O(n)

Given an array of strings strs, group the anagrams together.

Medium
Array & Hashing
High
Amazon
Facebook
Microsoft
Uber
Time: O(n * m log m)
Space: O(n * m)

Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].

Medium
Array & Hashing
Very High
Amazon
Microsoft
Facebook
Apple
Time: O(n)
Space: O(1)

Design an algorithm to encode a list of strings to a string and decode it back.

Medium
Array & Hashing
High
Google
Facebook
Amazon
Time: O(n)
Space: O(n)

Given an integer array nums and an integer k, return the k most frequent elements.

Medium
Array & Hashing
High
Amazon
Facebook
Microsoft
Yelp
Time: O(n log k)
Space: O(n + k)

Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.

Medium
Array & Hashing
High
Google
Facebook
Amazon
Time: O(n)
Space: O(n)

Given a string s, return the longest palindromic substring in s.

Medium
Array & Hashing
Very High
Amazon
Google
Meta
Microsoft
Time: O(n²)
Space: O(1)

Given an unsorted integer array nums, return the smallest missing positive integer.

Hard
Array & Hashing
High
Amazon
Google
Meta
Time: O(n)
Space: O(1)

Given an m x n matrix, return all elements of the matrix in spiral order.

Medium
Array & Hashing
High
Amazon
Google
Meta
Time: O(m*n)
Space: O(1)

Rotate array to the right by k steps.

Medium
Array & Hashing
High
Amazon
Google
Microsoft
Time: O(n)
Space: O(1)

Find all elements that appear twice in an array.

Medium
Array & Hashing
Medium
Amazon
Google
Time: O(n)
Space: O(1)

Find the contiguous subarray with the largest sum.

Medium
Array & Hashing
Very High
Amazon
Google
Meta
Time: O(n)
Space: O(1)

Merge nums2 into nums1 in sorted order.

Easy
Array & Hashing
High
Amazon
Google
Meta
Time: O(m + n)
Space: O(1)

A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward.

Easy
Two Pointers
Very High
Microsoft
Facebook
Amazon
Palantir
Time: O(n)
Space: O(1)

3Sum

LC 15

Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.

Medium
Two Pointers
Very High
Facebook
Amazon
Microsoft
Adobe
Time: O(n²)
Space: O(1)

You are given an integer array height of length n. Find two lines that together with the x-axis form a container that can hold the most water.

Medium
Two Pointers
Very High
Amazon
Facebook
Microsoft
Bloomberg
Time: O(n)
Space: O(1)

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.

Hard
Two Pointers
Very High
Amazon
Facebook
Microsoft
Google
+1 more
Time: O(n)
Space: O(1)

Given the head of a linked list, remove the nth node from the end of the list and return its head.

Medium
Linked List
Very High
Amazon
Microsoft
Facebook
Apple
Time: O(n)
Space: O(1)

You are given the head of a singly linked-list. Reorder the list to be L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → …

Medium
Linked List
High
Amazon
Facebook
Microsoft
Time: O(n)
Space: O(1)

Given an integer array sorted in non-decreasing order, return array of squares in sorted order.

Easy
Two Pointers
Medium
Amazon
Google
Meta
Time: O(n)
Space: O(n)

Sort an array with only 0s, 1s, and 2s in-place.

Medium
Two Pointers
High
Amazon
Google
Meta
Time: O(n)
Space: O(1)

Remove duplicates such that each unique element appears at most twice.

Medium
Two Pointers
Medium
Amazon
Google
Time: O(n)
Space: O(1)

Compare two strings after processing backspaces (#).

Easy
Two Pointers
Medium
Google
Meta
Microsoft
Time: O(m + n)
Space: O(1)

Find minimum length of subarray with sum >= target.

Medium
Two Pointers
High
Amazon
Google
Meta
Time: O(n)
Space: O(1)

Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.

Easy
Two Pointers
High
Amazon
Microsoft
Google
Time: O(n)
Space: O(1)

Given an integer array nums and an integer val, remove all occurrences of val in nums in-place.

Easy
Two Pointers
Medium
Amazon
Microsoft
Google
Time: O(n)
Space: O(1)

Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

Easy
Stack
Very High
Amazon
Microsoft
Facebook
Google
+1 more
Time: O(n)
Space: O(n)

Evaluate the value of an arithmetic expression in Reverse Polish Notation.

Medium
Stack
Medium
Amazon
LinkedIn
Microsoft
Time: O(n)
Space: O(n)

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

Medium
Stack
High
Amazon
Microsoft
Facebook
Google
Time: O(4^n / √n)
Space: O(4^n / √n)

Min Stack

LC 155

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

Medium
Stack
Very High
Amazon
Microsoft
Bloomberg
Google
Time: O(1)
Space: O(n)

Find the area of the largest rectangle in histogram.

Hard
Stack
High
Amazon
Google
Meta
Time: O(n)
Space: O(n)

Find how many days until warmer temperature for each day.

Medium
Stack
High
Amazon
Google
Meta
Time: O(n)
Space: O(n)

Find next greater element for each element in nums1 using nums2.

Easy
Stack
Medium
Amazon
Google
Time: O(m + n)
Space: O(n)

Remove k digits from number to make smallest possible number.

Medium
Stack
Medium
Amazon
Google
Meta
Time: O(n)
Space: O(n)

Decode string with pattern k[encoded_string].

Medium
Stack
High
Amazon
Google
Meta
Time: O(n)
Space: O(n)

Find the final state after all asteroid collisions.

Medium
Stack
Medium
Amazon
Google
Time: O(n)
Space: O(n)

You are given an array prices where prices[i] is the price of a given stock on the ith day. Find the maximum profit.

Easy
Sliding Window
Very High
Amazon
Facebook
Microsoft
Bloomberg
+1 more
Time: O(n)
Space: O(1)

Given a string s, find the length of the longest substring without repeating characters.

Medium
Sliding Window
Very High
Amazon
Microsoft
Facebook
Adobe
Time: O(n)
Space: O(min(m,n))

You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most k times. Return the length of the longest substring containing the same letter you can get after performing the above operations.

Medium
Sliding Window
High
Microsoft
Amazon
Facebook
Time: O(n)
Space: O(1)

Given two strings s and t, return the minimum window substring of s such that every character in t is included in the window.

Hard
Sliding Window
High
Facebook
Amazon
Microsoft
Uber
Time: O(n + m)
Space: O(m)

Given two strings s1 and s2, return true if s2 contains a permutation of s1.

Medium
Sliding Window
Medium
Microsoft
Facebook
Amazon
Time: O(n + m)
Space: O(m)

Given two strings s and p, return an array of all the start indices of p's anagrams in s.

Medium
Sliding Window
Medium
Amazon
Facebook
Microsoft
Time: O(n)
Space: O(1)

Return the maximum element in each sliding window of size k.

Hard
Sliding Window
High
Amazon
Google
Meta
Time: O(n)
Space: O(k)

Find the maximum number of fruits you can collect with at most 2 types.

Medium
Sliding Window
Medium
Amazon
Google
Time: O(n)
Space: O(1)

Find max consecutive 1s if you can flip at most k zeros.

Medium
Sliding Window
Medium
Amazon
Google
Time: O(n)
Space: O(1)

Find all starting indices where concatenation of all words appears.

Hard
Sliding Window
Medium
Amazon
Google
Meta
Time: O(n * m)
Space: O(m)

Given the root of a binary tree, invert the tree, and return its root.

Easy
Binary Tree
Very High
Google
Amazon
Microsoft
Facebook
Time: O(n)
Space: O(h)

Given the root of a binary tree, return its maximum depth.

Easy
Binary Tree
Very High
Amazon
Microsoft
Facebook
LinkedIn
Time: O(n)
Space: O(h)

Same Tree

LC 100

Given the roots of two binary trees p and q, write a function to check if they are the same or not.

Easy
Binary Tree
High
Amazon
Microsoft
Bloomberg
Time: O(n)
Space: O(h)

Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST.

Medium
Binary Tree
High
Amazon
Microsoft
Facebook
LinkedIn
Time: O(h)
Space: O(h)

Given the root of a binary tree, return the level order traversal of its nodes' values.

Medium
Binary Tree
Very High
Amazon
Microsoft
Facebook
LinkedIn
Time: O(n)
Space: O(w)

Given the root of a binary tree, determine if it is a valid binary search tree (BST).

Medium
Binary Tree
Very High
Amazon
Microsoft
Facebook
Bloomberg
Time: O(n)
Space: O(h)

Given the root of a binary search tree, and an integer k, return the kth smallest value (1-indexed) of all the values of the nodes in the tree.

Medium
Binary Tree
High
Amazon
Microsoft
Facebook
Google
Time: O(h + k)
Space: O(h)

Given the roots of two binary trees root and subRoot, return true if there is a subtree of root with the same structure and node values of subRoot.

Easy
Binary Tree
High
Amazon
Facebook
Microsoft
Time: O(m * n)
Space: O(h)

Given two integer arrays preorder and inorder, construct and return the binary tree.

Medium
Binary Tree
High
Amazon
Microsoft
Facebook
LinkedIn
Time: O(n)
Space: O(n)

Design an algorithm to serialize and deserialize a binary tree.

Hard
Binary Tree
Very High
Amazon
Facebook
Microsoft
LinkedIn
Time: O(n)
Space: O(n)

Path Sum

LC 112

Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum.

Easy
Binary Tree
High
Amazon
Microsoft
Google
Time: O(n)
Space: O(h)

You are given the root of a binary tree containing digits from 0 to 9 only. Each root-to-leaf path in the tree represents a number.

Medium
Binary Tree
Medium
Amazon
Microsoft
Google
Time: O(n)
Space: O(h)

You are climbing a staircase. It takes n steps to reach the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

Easy
Dynamic Programming
Very High
Amazon
Adobe
Apple
Time: O(n)
Space: O(1)

You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money. Return the fewest number of coins that you need to make up that amount.

Medium
Dynamic Programming
Very High
Amazon
Microsoft
Facebook
Uber
Time: O(amount * coins)
Space: O(amount)

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected.

Medium
Dynamic Programming
Very High
Amazon
Microsoft
Google
Time: O(n)
Space: O(1)

Given an integer array nums, return the length of the longest strictly increasing subsequence.

Medium
Dynamic Programming
High
Microsoft
Amazon
Facebook
Time: O(n log n)
Space: O(n)

Given a string s and a dictionary of strings wordDict, return true if s can be segmented into a space-separated sequence of one or more dictionary words.

Medium
Dynamic Programming
Very High
Amazon
Facebook
Microsoft
Google
Time: O(n^2)
Space: O(n)

A message containing letters from A-Z can be encoded into numbers using the mapping A=1, B=2, ..., Z=26. Given a string s containing only digits, return the number of ways to decode it.

Medium
Dynamic Programming
High
Amazon
Microsoft
Facebook
Uber
Time: O(n)
Space: O(1)

There is a robot on an m x n grid. The robot can only move either down or right. How many possible unique paths are there?

Medium
Dynamic Programming
High
Amazon
Microsoft
Facebook
Bloomberg
Time: O(m * n)
Space: O(n)

You are given an integer array nums. You are initially positioned at the array's first index. Each element represents your maximum jump length. Return true if you can reach the last index.

Medium
Dynamic Programming
High
Amazon
Microsoft
Google
Time: O(n)
Space: O(1)

Given the head of a singly linked list, reverse the list, and return the reversed list.

Easy
Linked List
Very High
Amazon
Microsoft
Facebook
Apple
Time: O(n)
Space: O(1)

Given head, the head of a linked list, determine if the linked list has a cycle in it.

Easy
Linked List
Very High
Amazon
Microsoft
Yahoo
Time: O(n)
Space: O(1)

You are given the heads of two sorted linked lists list1 and list2. Merge the two lists into one sorted list.

Easy
Linked List
Very High
Amazon
Microsoft
Apple
Adobe
Time: O(n + m)
Space: O(1)

Add two numbers represented as linked lists in reverse order.

Medium
Linked List
Very High
Amazon
Google
Meta
Time: O(max(m, n))
Space: O(max(m, n))

Find the node at which two linked lists intersect.

Easy
Linked List
High
Amazon
Google
Meta
Time: O(m + n)
Space: O(1)

Check if linked list is a palindrome.

Easy
Linked List
High
Amazon
Google
Meta
Time: O(n)
Space: O(1)

Remove all elements from linked list with given value.

Easy
Linked List
Medium
Amazon
Google
Time: O(n)
Space: O(1)

Deep copy linked list where each node has next and random pointer.

Medium
Linked List
High
Amazon
Google
Meta
Time: O(n)
Space: O(1)

Flatten multilevel doubly linked list to single level.

Medium
Linked List
Medium
Amazon
Google
Time: O(n)
Space: O(d)

Write a function to delete a node in a singly-linked list. You will not be given access to the head of the list.

Easy
Linked List
Medium
Amazon
Microsoft
Google
Time: O(1)
Space: O(1)

Given an m x n 2D binary grid which represents a map of '1's (land) and '0's (water), return the number of islands.

Medium
Graphs
Very High
Amazon
Microsoft
Facebook
Google
Time: O(m * n)
Space: O(m * n)

Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph.

Medium
Graphs
High
Amazon
Facebook
Microsoft
Google
Time: O(V + E)
Space: O(V)

There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai.

Medium
Graphs
High
Amazon
Microsoft
Google
Time: O(V + E)
Space: O(V)

There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. Given prerequisites array, return true if you can finish all courses.

Medium
Graphs
High
Facebook
Amazon
Zenefits
Time: O(V + E)
Space: O(V + E)

Given an m x n rectangular island that borders both the Pacific and Atlantic oceans, return coordinates that can flow to both oceans.

Medium
Graphs
Medium
Google
Facebook
Amazon
Time: O(m * n)
Space: O(m * n)

Given an integer array nums and an integer k, return the kth largest element in the array.

Medium
Heap / Priority Queue
Very High
Amazon
Facebook
Microsoft
Apple
Time: O(n log k)
Space: O(k)

The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value and the median is the mean of the two middle values.

Hard
Heap / Priority Queue
Very High
Amazon
Facebook
Microsoft
Google
Time: O(log n)
Space: O(n)

You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. Merge all the linked-lists into one sorted linked-list and return it.

Hard
Heap / Priority Queue
Very High
Amazon
Facebook
Microsoft
Google
Time: O(n log k)
Space: O(k)

Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target.

Medium
Backtracking
High
Amazon
Facebook
Microsoft
Airbnb
Time: O(N^(T/M))
Space: O(T/M)

Given an m x n grid of characters board and a string word, return true if word exists in the grid.

Medium
Backtracking
High
Amazon
Microsoft
Facebook
Bloomberg
Time: O(N * 4^L)
Space: O(L)

Given an array nums of distinct integers, return all the possible permutations.

Medium
Backtracking
High
Amazon
Microsoft
Google
Facebook
Time: O(n!)
Space: O(n)

Subsets

LC 78

Given an integer array nums of unique elements, return all possible subsets (the power set).

Medium
Backtracking
High
Amazon
Microsoft
Facebook
Google
Time: O(2^n)
Space: O(n)

Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals.

Medium
Intervals
Very High
Facebook
Amazon
Microsoft
Google
+1 more
Time: O(n log n)
Space: O(n)

Given an array of intervals, return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.

Medium
Intervals
High
Amazon
Microsoft
Facebook
Time: O(n log n)
Space: O(1)

Given an array of meeting time intervals, find the minimum number of conference rooms required.

Medium
Intervals
High
Amazon
Microsoft
Facebook
Google
Time: O(n log n)
Space: O(n)

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).

Medium
Intervals
High
Amazon
Microsoft
Google
Time: O(n)
Space: O(n)

Write a function that takes an unsigned integer and returns the number of '1' bits it has.

Easy
Math & Bit Manipulation
High
Amazon
Microsoft
Apple
Time: O(1)
Space: O(1)

Reverse bits of a given 32 bits unsigned integer.

Easy
Math & Bit Manipulation
Medium
Amazon
Apple
Airbnb
Time: O(1)
Space: O(1)

Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.

Easy
Math & Bit Manipulation
High
Amazon
Microsoft
Bloomberg
Time: O(n)
Space: O(1)

Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.

Easy
Math & Bit Manipulation
High
Amazon
Microsoft
Facebook
Time: O(n)
Space: O(1)

Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1's in the binary representation of i.

Easy
Math & Bit Manipulation
Medium
Amazon
Microsoft
Google
Time: O(n)
Space: O(n)

Given two integers a and b, return the sum of the two integers without using the operators + and -.

Medium
Math & Bit Manipulation
Medium
Amazon
Microsoft
Google
Time: O(1)
Space: O(1)

Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums.

Easy
Binary Search
Very High
Amazon
Microsoft
Facebook
Google
Time: O(log n)
Space: O(1)

You are given an integer array nums sorted in ascending order (with distinct values), and an integer target. The array is rotated at some pivot. Search for target and return its index.

Medium
Binary Search
Very High
Amazon
Microsoft
Facebook
LinkedIn
Time: O(log n)
Space: O(1)

Suppose an array of length n sorted in ascending order is rotated. Find the minimum element.

Medium
Binary Search
High
Amazon
Microsoft
Facebook
Time: O(log n)
Space: O(1)

Find the index where target should be inserted in sorted array.

Easy
Binary Search
High
Amazon
Google
Time: O(log n)
Space: O(1)

Find the first bad version to minimize API calls.

Easy
Binary Search
High
Meta
Amazon
Time: O(log n)
Space: O(1)

Find starting and ending position of target in sorted array.

Medium
Binary Search
High
Amazon
Google
Meta
Time: O(log n)
Space: O(1)

Search for target in sorted 2D matrix.

Medium
Binary Search
High
Amazon
Google
Meta
Time: O(log(m*n))
Space: O(1)

Find a peak element where nums[i] > nums[i-1] and nums[i] > nums[i+1].

Medium
Binary Search
High
Amazon
Google
Meta
Time: O(log n)
Space: O(1)

Find the kth largest element in an unsorted array.

Medium
Binary Search
Very High
Amazon
Google
Meta
Time: O(n)
Space: O(1)

Find median of two sorted arrays.

Hard
Binary Search
Very High
Amazon
Google
Meta
Time: O(log(min(m,n)))
Space: O(1)

Implement a trie with insert, search, and startsWith methods.

Medium
Tries
Very High
Amazon
Microsoft
Facebook
Google
Time: O(m)
Space: O(ALPHABET_SIZE * N * M)

Design a data structure that supports adding new words and finding if a string matches any previously added string. Words may contain dots '.' as wildcards.

Medium
Tries
High
Amazon
Facebook
Microsoft
Time: O(m)
Space: O(ALPHABET_SIZE * N * M)

Given an m x n board of characters and a list of strings words, return all words on the board.

Hard
Tries
High
Amazon
Microsoft
Facebook
Google
Time: O(m * n * 4^k)
Space: O(k)

Given an integer array nums, find the contiguous subarray with the largest sum, and return its sum.

Medium
Greedy
Very High
Amazon
Microsoft
Facebook
LinkedIn
Time: O(n)
Space: O(1)

There are n gas stations along a circular route. Given two arrays gas and cost, return the starting gas station's index if you can travel around the circuit once.

Medium
Greedy
Medium
Amazon
Facebook
Google
Time: O(n)
Space: O(1)

You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position.

Medium
Greedy
High
Amazon
Microsoft
Google
Time: O(n)
Space: O(1)

Candy

LC 135

There are n children standing in a line. Each child is assigned a rating value given in the integer array ratings.

Hard
Greedy
Medium
Amazon
Microsoft
Google
Time: O(n)
Space: O(n)

You are given a network of n nodes. Given times array and integers n and k, return the time it takes for all nodes to receive the signal sent from node k.

Medium
Advanced Graphs
Medium
Amazon
Google
Facebook
Time: O(E log V)
Space: O(V + E)

Given a list of accounts where each element accounts[i] contains the account name and a list of emails, merge accounts that belong to the same person.

Medium
Advanced Graphs
Medium
Facebook
Amazon
Google
Time: O(N log N)
Space: O(N)

LRU Cache

LC 146

Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.

Medium
System Design
Very High
Amazon
Google
Meta
Time: O(1)
Space: O(capacity)

Design a simplified version of Twitter with basic functionality.

Medium
System Design
High
Amazon
Google
Meta
Time: O(k log n)
Space: O(n)

Design a hit counter which counts the number of hits received in the past 5 minutes.

Medium
System Design
Medium
Amazon
Google
Microsoft
Time: O(1)
Space: O(300)

Design an underground system to track customer travel times between stations.

Medium
System Design
Medium
Amazon
Google
Time: O(1)
Space: O(n)