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.
Filters
Contains Duplicate
Given an integer array nums, return true if any value appears at least twice in the array.
Valid Anagram
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
Two Sum
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
Group Anagrams
Given an array of strings strs, group the anagrams together.
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].
Design an algorithm to encode a list of strings to a string and decode it back.
Top K Frequent Elements
Given an integer array nums and an integer k, return the k most frequent elements.
Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.
Given a string s, return the longest palindromic substring in s.
Given an unsorted integer array nums, return the smallest missing positive integer.
Spiral Matrix
Given an m x n matrix, return all elements of the matrix in spiral order.
Rotate Array
Rotate array to the right by k steps.
Find all elements that appear twice in an array.
Maximum Subarray
Find the contiguous subarray with the largest sum.
Merge Sorted Array
Merge nums2 into nums1 in sorted order.
Valid Palindrome
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.
3Sum
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.
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.
Trapping Rain Water
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.
Given the head of a linked list, remove the nth node from the end of the list and return its head.
Reorder List
You are given the head of a singly linked-list. Reorder the list to be L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → …
Given an integer array sorted in non-decreasing order, return array of squares in sorted order.
Sort Colors
Sort an array with only 0s, 1s, and 2s in-place.
Remove duplicates such that each unique element appears at most twice.
Backspace String Compare
Compare two strings after processing backspaces (#).
Find minimum length of subarray with sum >= target.
Move Zeroes
Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.
Remove Element
Given an integer array nums and an integer val, remove all occurrences of val in nums in-place.
Valid Parentheses
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
Find the area of the largest rectangle in histogram.
Daily Temperatures
Find how many days until warmer temperature for each day.
Next Greater Element I
Find next greater element for each element in nums1 using nums2.
Remove K Digits
Remove k digits from number to make smallest possible number.
Asteroid Collision
Find the final state after all asteroid collisions.
You are given an array prices where prices[i] is the price of a given stock on the ith day. Find the maximum profit.
Given a string s, find the length of the longest substring without repeating characters.
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.
Given two strings s and t, return the minimum window substring of s such that every character in t is included in the window.
Permutation in String
Given two strings s1 and s2, return true if s2 contains a permutation of s1.
Given two strings s and p, return an array of all the start indices of p's anagrams in s.
Sliding Window Maximum
Return the maximum element in each sliding window of size k.
Fruit Into Baskets
Find the maximum number of fruits you can collect with at most 2 types.
Max Consecutive Ones III
Find max consecutive 1s if you can flip at most k zeros.
Find all starting indices where concatenation of all words appears.
Invert Binary Tree
Given the root of a binary tree, invert the tree, and return its root.
Given the root of a binary tree, return its maximum depth.
Same Tree
Given the roots of two binary trees p and q, write a function to check if they are the same or not.
Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST.
Given the root of a binary tree, return the level order traversal of its nodes' values.
Given the root of a binary tree, determine if it is a valid binary search tree (BST).
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.
Subtree of Another Tree
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.
Given two integer arrays preorder and inorder, construct and return the binary tree.
Design an algorithm to serialize and deserialize a binary tree.
Path Sum
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.
Sum Root to Leaf Numbers
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.
Climbing Stairs
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?
Coin Change
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.
House Robber
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.
Given an integer array nums, return the length of the longest strictly increasing subsequence.
Word Break
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.
Decode Ways
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.
Unique Paths
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?
Jump Game
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.
Reverse Linked List
Given the head of a singly linked list, reverse the list, and return the reversed list.
Linked List Cycle
Given head, the head of a linked list, determine if the linked list has a cycle in it.
You are given the heads of two sorted linked lists list1 and list2. Merge the two lists into one sorted list.
Add Two Numbers
Add two numbers represented as linked lists in reverse order.
Find the node at which two linked lists intersect.
Palindrome Linked List
Check if linked list is a palindrome.
Remove all elements from linked list with given value.
Deep copy linked list where each node has next and random pointer.
Flatten multilevel doubly linked list to single level.
Write a function to delete a node in a singly-linked list. You will not be given access to the head of the list.
Number of Islands
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.
Clone Graph
Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph.
Course Schedule II
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.
Course Schedule
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.
Given an m x n rectangular island that borders both the Pacific and Atlantic oceans, return coordinates that can flow to both oceans.
Given an integer array nums and an integer k, return the kth largest element in the array.
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.
Merge k Sorted Lists
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.
Combination Sum
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.
Word Search
Given an m x n grid of characters board and a string word, return true if word exists in the grid.
Permutations
Given an array nums of distinct integers, return all the possible permutations.
Subsets
Given an integer array nums of unique elements, return all possible subsets (the power set).
Merge Intervals
Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals.
Given an array of intervals, return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
Meeting Rooms II
Given an array of meeting time intervals, find the minimum number of conference rooms required.
Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of '1' bits it has.
Reverse Bits
Reverse bits of a given 32 bits unsigned integer.
Missing Number
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.
Single Number
Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.
Counting Bits
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.
Sum of Two Integers
Given two integers a and b, return the sum of the two integers without using the operators + and -.
Binary Search
Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums.
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.
Suppose an array of length n sorted in ascending order is rotated. Find the minimum element.
Find the index where target should be inserted in sorted array.
First Bad Version
Find the first bad version to minimize API calls.
Find starting and ending position of target in sorted array.
Search a 2D Matrix
Search for target in sorted 2D matrix.
Find Peak Element
Find a peak element where nums[i] > nums[i-1] and nums[i] > nums[i+1].
Find the kth largest element in an unsorted array.
Find median of two sorted arrays.
Implement a trie with insert, search, and startsWith methods.
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.
Word Search II
Given an m x n board of characters and a list of strings words, return all words on the board.
Maximum Subarray
Given an integer array nums, find the contiguous subarray with the largest sum, and return its sum.
Gas Station
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.
Jump Game
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.
Candy
There are n children standing in a line. Each child is assigned a rating value given in the integer array ratings.
Network Delay Time
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.
Accounts Merge
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.
LRU Cache
Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.
Design Twitter
Design a simplified version of Twitter with basic functionality.
Design Hit Counter
Design a hit counter which counts the number of hits received in the past 5 minutes.
Design Underground System
Design an underground system to track customer travel times between stations.