100 Most Asked Leetcode Questions
100 Most Asked Leetcode Questions
Most-liked
Questions
*Disclaimer*
No list alone can guarantee mastery.
1
Backtracking
1. Letter Combinations of a Phone
Number (Medium)
Given a string containing digits from 2-9 inclusive, return all
possible letter combinations that the number could represent.
Return the answer in any order.
2
Example 1:
Input: n = 3
Output: ["((()))","(()())","(())()","()(())","()()()"]
Example 2:
Input: n = 1
Output: ["()"]
Constraints:
1 <= n <= 8
Practice
The test cases are generated such that the number of unique
combinations that sum up to the target is less than 150
combinations for the given input.
Practice
3
4. Permutations (Medium)
Given an array nums of distinct integers, return all the possible
permutations. You can return the answer in any order.
Practice
5. N-Queens (Hard)
The n-queens puzzle is the problem of placing n queens on an n x
n chessboard such that no two queens attack each other.
Practice
4
Subsets (Medium)
Given an integer array nums of unique elements, return all
possible subsets (the power set).
The solution set must not contain duplicate subsets. Return the
solution in any order.
Practice
Practice
Practice
5
Binary Search
Median of Two Sorted Arrays
(Hard)
Given two sorted arrays nums1 and nums2 of size m and n
respectively, return the median of the two sorted arrays.
Practice
6
Given the array nums after the possible rotation and an integer
target, return the index of target if it is in nums, or -1 if it is not in
nums.
7
13. Search a 2D Matrix (Medium)
You are given an m x n integer matrix matrix with the following two
properties:
The first integer of each row is greater than the last integer of the
previous row.
Practice
8
14. Binary Tree Maximum Path Sum
(Hard)
A path in a binary tree is a sequence of nodes where each pair of
adjacent nodes in the sequence has an edge connecting them. A
node can only appear in the sequence at most once. Note that the
path does not need to pass through the root.
The path sum of a path is the sum of the node's values in the
path.
Given the root of a binary tree, return the maximum path sum of
any non-empty path.
Practice
9
15. Find Minimum in Rotated Sorted
Array (Medium)
Suppose an array of length n sorted in ascending order is rotated
between 1 and n times. For example, the array nums =
[0,1,2,4,5,6,7] might become:
Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time
results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].
Practice
10
Binary Tree
16. Binary Tree Inorder Traversal
(Easy)
Given the root of a binary tree, return the inorder traversal of its
nodes' values.
Practice
The left subtree of a node contains only nodes with keys strictly
less than the node's key.
The right subtree of a node contains only nodes with keys strictly
greater than the node's key.
Both the left and right subtrees must also be binary search trees.
Practice
11
18.Symmetric Tree (Easy)
Given the root of a binary tree, check whether it is a mirror of
itself (i.e., symmetric around its center).
Practice
Practice
Practice
12
21. Construct Binary Tree from
Preorder and Inorder Traversal
(Medium)
Given two integer arrays preorder and inorder where preorder is
the preorder traversal of a binary tree and inorder is the inorder
traversal of the same tree, construct and return the binary tree.
Practice
13
23. Flatten Binary Tree to Linked List
(Medium)
Given the root of a binary tree, flatten the tree into a "linked list":
The "linked list" should use the same TreeNode class where the
right child pointer points to the next node in the list and the left
child pointer is always null.
Practice
Practice
14
25.Invert Binary Tree (Easy)
Given the root of a binary tree, invert the tree, and return its root.
Practice
Practice
15
27. Lowest Common Ancestor of a
Binary Tree (Medium)
Given a binary tree, find the lowest common ancestor (LCA) of
two given nodes in the tree.
Practice
The path does not need to start or end at the root or a leaf, but it
must go downwards (i.e., traveling only from parent nodes to child
nodes).
Practice
16
29. Diameter of Binary Tree (Easy)
Given the root of a binary tree, return the length of the diameter
of the tree.
Practice
17
Dynamic Programming
30. Longest Palindromic Substring
(Medium)
Given a string s, return the longest palindromic substring in s.
18
32. Unique Paths (Medium)
There is a robot on an m x n grid. The robot is initially located at
the top-left corner (i.e., grid[0][0]). The robot tries to move to the
bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only
move either down or right at any point in time.
The test cases are generated so that the answer will be less than
or equal to 2 * 109.
Practice
Note: You can only move either down or right at any point in time.
Practice
19
34. Climbing Stairs (Easy)
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?
Practice
Insert a character
Delete a character
Replace a character
Practice
20
36. Pascal's Triangle (Easy)
Given an integer numRows, return the first numRows of Pascal's
triangle.
Practice
Note that the same word in the dictionary may be reused multiple
times in the segmentation.
Practice
21
38.Maximum Product Subarray
(Medium)
Given an integer array nums, find a subarray that has the largest
product, and return the product.
The test cases are generated so that the answer will fit in a 32-bit
integer.
Practice
Practice
22
Linked List
40. Reverse a Linked List
Given the head of a singly linked list, reverse the list, and return
the reversed list.
Practice
There is a cycle in a linked list if there is some node in the list that
can be reached again by continuously following the next pointer.
Internally, pos is used to denote the index of the node that tail's
next pointer is connected to. Note that pos is not passed as a
parameter.
Practice
23
42. Coin Change (Medium)
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. If that amount of money cannot be made up by any
combination of the coins, return -1.
You may assume that you have an infinite number of each kind of
coin.
Practice
Practice
24
44. Longest Common Subsequence
(Medium)
Given two strings text1 and text2, return the length of their
longest common subsequence. If there is no common
subsequence, return 0.
Practice
25
GRAPH
45. Number of Islands (Medium)
Given an m x n 2D binary grid grid which represents a map of '1's
(land) and '0's (water), return the number of islands.
For example, the pair [0, 1], indicates that to take course 0 you
have to first take course 1.
Return true if you can finish all courses. Otherwise, return false.
Practice
26
47. Rotting Oranges (Medium)
You are given an m x n grid where each cell can have one of three
values:
Practice
27
Greedy
48. Jump Game II (Medium)
You are given a 0-indexed array of integers nums of length n. You
are initially positioned at index 0.
i + j < n
28
49. Jump Game (Medium)
You are given an integer array nums. You are initially positioned at
the array's first index, and each element in the array represents
Return true if you can reach the last index, or false otherwise.
Practice
(Easy)
You are given an array prices where prices[i] is the price of a given
one stock and choosing a different day in the future to sell that
stock.
Return the maximum profit you can achieve from this transaction.
Practice
29
51. Partition Labels (Medium)
You are given a string s. We want to partition the string into as
many parts as possible so that each letter appears in at most one
part. For example, the string "ababcc" can be partitioned into
["abab", "cc"], but partitions such as ["aba", "bcc"] or ["ab", "ab",
"cc"] are invalid.
Note that the partition is done so that after concatenating all the
parts in order, the resultant string should be s.
Practice
30
Hashing
You may assume that each input would have exactly one solution,
and you may not use the same element twice.
Practice
Practice
31
54. Longest Consecutive Sequence
(Medium)
Given an unsorted array of integers nums, return the length of the
longest consecutive elements sequence.
Practice
Practice
32
HeAP
56. Kth Largest Element in an Array
(Medium)
Given an integer array nums and an integer k, return the kth
largest element in the array.
Note that it is the kth largest element in the sorted order, not the
kth distinct element.
Practice
33
57. Find Median from Data Stream
(Hard)
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.
void addNum(int num) adds the integer num from the data
stream to the data structure.
Practice
Practice
34
Linked Lists
59. Add Two Numbers (Medium)
You are given two non-empty linked lists representing two non-
negative integers. The digits are stored in reverse order, and each
of their nodes contains a single digit. Add the two numbers and
return the sum as a linked list.
You may assume the two numbers do not contain any leading
zero, except the number 0 itself.
Practice
Practice
35
61. Merge Two Sorted Lists (Easy)
You are given the heads of two sorted linked lists list1 and list2.
Merge the two lists into one sorted list. The list should be made by
splicing together the nodes of the first two lists.
Practice
Merge all the linked-lists into one sorted linked-list and return it.
Practice
Practice
36
64. Reverse Nodes in k-Group (Hard)
Given the head of a linked list, reverse the nodes of the list k at a
time, and return the modified list.
You may not alter the values in the list's nodes, only nodes
themselves may be changed.
Practice
37
65.Copy List with Random Pointer
(Medium)
A linked list of length n is given such that each node contains an
additional random pointer, which could point to any node in the
list, or null.
Construct a deep copy of the list. The deep copy should consist of
exactly n brand new nodes, where each new node has its value
set to the value of its corresponding original node. Both the next
and random pointer of the new nodes should point to new nodes
in the copied list such that the pointers in the original list and
copied list represent the same list state. None of the pointers in
the new list should point to nodes in the original list.
For example, if there are two nodes X and Y in the original list,
where X.random --> Y, then for the corresponding two nodes x
and y in the copied list, x.random --> y.
Your code will only be given the head of the original linked list.
Practice
38
66. Linked List Cycle (Easy)
Given head, the head of a linked list, determine if the linked list
has a cycle in it.
There is a cycle in a linked list if there is some node in the list that
can be reached again by continuously following the next pointer.
Internally, pos is used to denote the index of the node that tail's
next pointer is connected to. Note that pos is not passed as a
parameter.
Practice
There is a cycle in a linked list if there is some node in the list that
can be reached again by continuously following the next pointer.
Internally, pos is used to denote the index of the node that tail's
next pointer is connected to (0-indexed). It is -1 if there is no
cycle. Note that pos is not passed as a parameter.
Practice
39
68. LRU Cache (Medium)
size capacity.
int get(int key) Return the value of the key if the key exists,
void put(int key, int value) Update the value of the key if the
The functions get and put must each run in O(1) average time
complexity.
Practice
Given the head of a linked list, return the list after sorting it in
ascending order.
Practice
40
70. Intersection of Two Linked Lists
(Easy)
Given the heads of two singly linked-lists headA and headB, return
the node at which the two lists intersect. If the two linked lists
have no intersection at all, return null.
Note that the linked lists must retain their original structure after
the function returns.
Custom Judge:
The inputs to the judge are given as follows (your program is not
given these inputs):
The judge will then create the linked structure based on these
inputs and pass the two heads, headA and headB to your
program. If you correctly return the intersected node, then your
solution will be accepted.
Follow up: Could you write a solution that runs in O(m + n) time
and use only O(1) memory?
Practice
41
71. Reverse Linked List (Easy)
Given the head of a singly linked list, reverse the list, and return
the reversed list.
Practice
Practice
42
Matrix
73. Rotate Image (Medium)
You are given an n x n 2D matrix representing an image, rotate the
image by 90 degrees (clockwise).
You have to rotate the image in-place, which means you have to
modify the input 2D matrix directly. DO NOT allocate another 2D
matrix and do the rotation..
Practice
43
75. Set Matrix Zeroes (Medium)
Given an m x n integer matrix matrix, if an element is 0, set its
entire row and column to 0's. You must do it in place.
Follow up:
Practice
Practice
44
Sliding Window
77.Longest Substring Without
Repeating Characters (Medium)
Given a string s, find the length of the longest substring without
duplicate characters.
The test cases will be generated such that the answer is unique.
Practice
45
79. Sliding Window Maximum (Hard)
You are given an array of integers nums, there is a sliding window
of size k which is moving from the very left of the array to the very
right. You can only see the k numbers in the window. Each time
the sliding window moves right by one position.
Practice
Practice
46
Stack
81. Valid Parentheses (Easy)
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']',
determine if the input string is valid.
47
83. Min Stack (Medium)
Design a stack that supports push, pop, top, and retrieving the
minimum element in constant time.
void push(int val) pushes the element val onto the stack.
You must implement a solution with O(1) time complexity for each
function.
Practice
48
84. Decode String (Medium)
Given an encoded string, return its decoded string.
You may assume that the input string is always valid; there are no
extra white spaces, square brackets are well-formed, etc.
Furthermore, you may assume that the original data does not
contain any digits and that digits are only for those repeat
numbers, k. For example, there will not be input like 3a or 2[4].
The test cases are generated so that the length of the output will
never exceed 105.
Practice
Practice
49
Two Pointers
(Medium)
vertical lines drawn such that the two endpoints of the ith line are
Find two lines that together with the x-axis form a container, such
Practice
nums[j] + nums[k] == 0.
Notice that the solution set must not contain duplicate triplets.
Practice
50
88. Trapping Rain Water (Hard)
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.
Practice
Note that you must do this in-place without making a copy of the
array.
Practice
51
Trie
90. Implement Trie [Prefix Tree]
(Medium)
A trie (pronounced as "try") or prefix tree is a tree data structure
used to efficiently store and retrieve keys in a dataset of strings.
There are various applications of this data structure, such as
autocomplete and spellchecker.
void insert(String word) Inserts the string word into the trie.
Practice
52
Misc
91. Next Permutation (Medium)
A permutation of an array of integers is an arrangement of its
members into a sequence or linear order.
For example, for arr = [1,2,3], the following are all the
permutations of arr: [1,2,3], [1,3,2], [2, 1, 3], [2, 3, 1], [3,1,2],
[3,2,1].
Practice
53
92. First Missing Positive (Hard)
Given an unsorted integer array nums. Return the smallest positive
integer that is not present in nums.
You must implement an algorithm that runs in O(n) time and uses
O(1) auxiliary space.
Practice
Practice
Practice
54
95. Sort Colors (Medium)
Given an array nums with n objects colored red, white, or blue,
sort them in-place so that objects of the same color are adjacent,
with the colors in the order red, white, and blue.
You must solve this problem without using the library's sort
function.
Practice
Practice
55
97. Majority Element (Easy)
Given an array nums of size n, return the majority element.
Practice
Practice
56
99.Product of Array Except Self
(Medium)
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].
You must write an algorithm that runs in O(n) time and without
using the division operation.
Practice
You must solve the problem without modifying the array nums and
using only constant extra space.
Practice
57
Why Bosscoder?
2200+ Alumni placed at Top Product-
based companies.
Explore More