This repository contains solutions to the Leetcode Top Interview 150 problems.
These problems are commonly asked in technical interviews and cover a wide range of topics, including data structures, algorithms, and problem-solving techniques.
Show some β€οΈ by starring β this repository if you like it!
Contact for work, email: [email protected]
- What is Leetcode? π§βπ»
- About the Top Interview 150 Collection Questions π
- Tips for Solving Problems π‘
- Repository Structure ποΈ
- How to Use This Repository π
- Resources π
Leetcode is a popular online platform that provides a collection of coding challenges. Software engineers and interview candidates widely use it to practice coding skills and prepare for technical interviews. Leetcode offers many problems to solve, classified by difficulty level and topic.
The Top Interview 150 collection on Leetcode is a curated set of 150 interview questions that top tech companies frequently ask. These questions are carefully selected to cover essential concepts and algorithms that interviewers expect candidates to be familiar with.
Id | Problem | Difficulty | Signal | Status |
---|---|---|---|---|
Array / String | ||||
1 | Merge Sorted Array | Easy | π’ | β |
2 | Remove Element | Easy | π’ | β |
3 | Remove Duplicates from Sorted Array | Easy | π’ | β |
4 | Remove Duplicates from Sorted Array II | Medium | π‘ | β |
5 | Majority Element | Easy | π’ | β |
6 | Rotate Array | Medium | π‘ | β |
7 | Best Time to Buy and Sell Stock | Easy | π’ | β |
8 | Best Time to Buy and Sell Stock II | Medium | π‘ | β |
9 | Jump Game | Medium | π‘ | β |
10 | Jump Game II | Medium | π‘ | β |
11 | H-Index | Medium | π‘ | β |
12 | Insert Delete GetRandom O(1) | Medium | π‘ | β |
13 | Product of Array Except Self | Medium | π‘ | β |
14 | Gas Station | Medium | π‘ | β |
15 | Candy | Hard | π΄ | β |
16 | Trapping Rain Water | Hard | π΄ | β |
17 | Roman to Integer | Easy | π’ | β |
18 | Integer to Roman | Medium | π‘ | β |
19 | Length of Last Word | Easy | π’ | β |
20 | Longest Common Prefix | Easy | π’ | β |
21 | Reverse Words in a String | Medium | π‘ | β |
22 | Zigzag Conversion | Medium | π‘ | β |
23 | Find the Index of the First Occurrence in a String | Easy | π’ | β |
24 | Text Justification | Hard | π΄ | β |
Two Pointers | ||||
25 | Valid Palindrome | Easy | π’ | β |
26 | Is Subsequence | Easy | π’ | β |
27 | Two Sum II - Input Array Is Sorted | Medium | π‘ | β |
28 | Container With Most Water | Medium | π‘ | β |
29 | 3Sum | Medium | π‘ | β |
Sliding Window | ||||
30 | Minimum Size Subarray Sum | Medium | π‘ | β |
31 | Longest Substring Without Repeating Characters | Medium | π‘ | β |
32 | Substring with Concatenation of All Words | Hard | π΄ | β |
33 | Minimum Window Substring | Hard | π΄ | β |
Matrix | ||||
34 | Valid Sudoku | Medium | π‘ | β |
35 | Spiral Matrix | Medium | π‘ | β |
36 | Rotate Image | Medium | π‘ | β |
37 | Set Matrix Zeroes | Medium | π‘ | β |
38 | Game of Life | Medium | π‘ | β |
Hashmap | ||||
39 | Ransom Note | Easy | π’ | β |
40 | Isomorphic Strings | Easy | π’ | β |
41 | Word Pattern | Easy | π’ | β |
42 | Valid Anagram | Easy | π’ | β |
43 | Group Anagrams | Medium | π‘ | β |
44 | Two Sum | Easy | π’ | β |
45 | Happy Number | Easy | π’ | β |
46 | Contains Duplicate II | Easy | π’ | β |
47 | Longest Consecutive Sequence | Medium | π‘ | β |
Intervals | ||||
48 | Summary Ranges | Easy | π’ | β |
49 | Merge Intervals | Medium | π‘ | β |
50 | Insert Interval | Medium | π‘ | β |
51 | Minimum Number of Arrows to Burst Balloons | Medium | π‘ | β |
Stack | ||||
52 | Valid Parentheses | Easy | π’ | β |
53 | Simplify Path | Medium | π‘ | β |
54 | Min Stack | Medium | π‘ | β |
55 | Evaluate Reverse Polish Notation | Medium | π‘ | β |
56 | Basic Calculator | Hard | π΄ | β |
Linked List | ||||
57 | Linked List Cycle | Easy | π’ | β |
58 | Add Two Numbers | Medium | π‘ | β |
59 | Merge Two Sorted Lists | Easy | π’ | β |
60 | Copy List with Random Pointer | Medium | π‘ | β |
61 | Reverse Linked List II | Medium | π‘ | β |
62 | Reverse Nodes in k-Group | Hard | π΄ | β |
63 | Remove Nth Node From End of List | Medium | π‘ | β |
64 | Remove Duplicates from Sorted List II | Medium | π‘ | β |
65 | Rotate List | Medium | π‘ | β |
66 | Partition List | Medium | π‘ | β |
67 | LRU Cache | Medium | π‘ | β |
Binary Tree General | ||||
68 | Maximum Depth of Binary Tree | Easy | π’ | β |
69 | Same Tree | Easy | π’ | β |
70 | Invert Binary Tree | Easy | π’ | β |
71 | Symmetric Tree | Easy | π’ | β |
72 | Construct Binary Tree from Preorder and Inorder Traversal | Medium | π‘ | β |
73 | Construct Binary Tree from Inorder and Postorder Traversal | Medium | π‘ | β |
74 | Populating Next Right Pointers in Each Node II | Medium | π‘ | β |
75 | Flatten Binary Tree to Linked List | Medium | π‘ | |
76 | Path Sum | Easy | π’ | |
77 | Sum Root to Leaf Numbers | Medium | π‘ | |
78 | Binary Tree Maximum Path Sum | Hard | π΄ | |
79 | Binary Search Tree Iterator | Medium | π‘ | |
80 | Count Complete Tree Nodes | Easy | π’ | |
81 | Lowest Common Ancestor of a Binary Tree | Medium | π‘ | |
Binary Tree BFS | ||||
8282 | Binary Tree Right Side View | Medium | π‘ | |
83 | Average of Levels in Binary Tree | Easy | π’ | |
84 | Binary Tree Level Order Traversal | Medium | π‘ | |
85 | Binary Tree Zigzag Level Order Traversal | Medium | π‘ | |
Binary Search Tree | ||||
86 | Minimum Absolute Difference in BST | Easy | π’ | |
87 | Kth Smallest Element in a BST | Medium | π‘ | |
88 | Validate Binary Search Tree | Medium | π‘ | |
Graph General | ||||
89 | Number of Islands | Medium | π‘ | |
90 | Surrounded Regions | Medium | π‘ | |
91 | Clone Graph | Medium | π‘ | |
92 | Evaluate Division | Medium | π‘ | |
93 | Course Schedule | Medium | π‘ | |
94 | Course Schedule II | Medium | π‘ | |
Graph BFS | ||||
95 | Snakes and Ladders | Medium | π‘ | |
96 | Minimum Genetic Mutation | Medium | π‘ | |
97 | Word Ladder | Hard | π΄ | |
Trie | ||||
98 | Implement Trie (Prefix Tree) | Medium | π‘ | |
99 | Design Add and Search Words Data Structure | Medium | π‘ | |
100 | Word Search II | Hard | π΄ | |
Backtracking | ||||
101 | Letter Combinations of a Phone Number | Medium | π‘ | |
102 | Combinations | Medium | π‘ | |
103 | Permutations | Medium | π‘ | |
104 | Combination Sum | Medium | π‘ | |
105 | N-Queens II | Hard | π΄ | |
106 | Generate Parentheses | Medium | π‘ | |
107 | Word Search | Medium | π‘ | |
Divide & Conquer | ||||
108 | Convert Sorted Array to Binary Search Tree | Easy | π’ | |
109 | Sort List | Medium | π‘ | |
110 | Construct Quad Tree | Medium | π‘ | |
111 | Merge k Sorted Lists | Hard | π΄ | |
Kadane's Algorithm | ||||
112 | Maximum Subarray | Medium | π‘ | |
113 | Maximum Sum Circular Subarray | Medium | π‘ | |
Binary Search | ||||
114 | Search Insert Position | Easy | π’ | |
115 | Search a 2D Matrix | Medium | π‘ | |
116 | Find Peak Element | Medium | π‘ | |
117 | Search in Rotated Sorted Array | Medium | π‘ | |
118 | Find First and Last Position of Element in Sorted Array | Medium | π‘ | |
119 | Find Minimum in Rotated Sorted Array | Medium | π‘ | |
120 | Median of Two Sorted Arrays | Hard | π΄ | |
Heap | ||||
121 | Kth Largest Element in an Array | Medium | π‘ | |
122 | IPO | Hard | π΄ | |
123 | Find K Pairs with Smallest Sums | Medium | π‘ | |
124 | Find Median from Data Stream | Hard | π΄ | |
Bit Manipulation | ||||
125 | Add Binary | Easy | π’ | |
126 | Reverse Bits | Easy | π’ | |
127 | Number of 1 Bits | Easy | π’ | |
128 | Single Number | Easy | π’ | |
129 | Single Number II | Medium | π‘ | |
130 | Bitwise AND of Numbers Range | Medium | π‘ | |
Math | ||||
131 | Palindrome Number | Easy | π’ | |
132 | Plus One | Easy | π’ | |
133 | Factorial Trailing Zeroes | Medium | π‘ | |
134 | Sqrt(x) | Easy | π’ | |
135 | Pow(x, n) | Medium | π‘ | |
136 | Max Points on a Line | Hard | π΄ | |
1D DP | ||||
137 | Climbing Stairs | Easy | π’ | |
138 | House Robber | Medium | π‘ | |
139 | Word Break | Medium | π‘ | |
140 | Coin Change | Medium | π‘ | |
141 | Longest Increasing Subsequence | Medium | π‘ | |
Multidimensional DP | ||||
142 | Triangle | Medium | π‘ | |
143 | Minimum Path Sum | Medium | π‘ | |
144 | Unique Paths II | Medium | π‘ | |
145 | Longest Palindromic Substring | Medium | π‘ | |
146 | Interleaving String | Medium | π‘ | |
147 | Edit Distance | Medium | π‘ | |
148 | Best Time to Buy and Sell Stock III | Hard | π΄ | |
149 | Best Time to Buy and Sell Stock IV | Hard | π΄ | |
150 | Maximal Square | Medium | π‘ |
SSome helpful strategies to tackle different types of problems in this repository:
Strategy | Description | Example Use Case |
---|---|---|
Heap for Top K Elements πΌ | Use a Heap to find the top K largest, smallest, or closest elements among N elements. Efficient for problems requiring constant updates. | Finding the top K largest numbers in a list. |
Binary Search or Two Pointers for Sorted Input πβ‘οΈβ¬ οΈ | Use Binary Search or Two Pointers for sorted arrays, lists, or matrices to optimize the solution. | Searching for an element in a sorted array. |
Backtracking or BFS for Combinations and Permutations ππ | Use Backtracking or BFS for problems requiring exploration of all combinations or permutations. | Finding all permutations of a set. |
BFS or DFS for Trees and Graphs π³π | Use BFS or DFS for tree or graph-related problems. | Searching a graph or binary tree. |
Stack for Converting Recursion to Iteration π | Convert recursion problems into iterative solutions using a stack to simulate the call stack. | Performing a recursive search without using recursion. |
Optimizing Array Problems π | Optimize array problems from O(nΒ²) to O(n) using a HashMap or Set, or to O(n log n) using sorting. | Finding duplicate elements in an array. |
Dynamic Programming for Optimization Problems π | Dynamic Programming is essential for problems that involve maximizing or minimizing values, using results from previous subproblems. | Finding the maximum value in a sequence. |
Trie for String Manipulation π€ | For multiple strings or searching for common substrings, use a Trie or HashMap. A Trie is the most suitable data structure when manipulating multiple strings. | Searching or manipulating multiple strings efficiently. |
Fast & Slow Pointers for Linked Lists ππββοΈπββοΈ | Use Fast & Slow Pointers to efficiently solve LinkedList problems with restricted memory usage, like detecting cycles or finding the middle element. | Detecting cycles or finding the middle of a linked list. |
This repository is organized by problem number, with each problem having its own directory.
Inside each problem directory, you will find the solution file(s) along with a README file that provides a problem description, constraints, and additional notes if necessary.
Navigate through the problem directories to explore solutions. Each directory contains the solution files, including explanations and sample input/output.
You can practice implementing your solutions and compare them with those provided.
Feel free to contribute by submitting your solutions or improvements. Open a pull request to share your contributions.
I recommend contributions for new languages, as currently, the solution is only available in Java π.
Happy coding and good luck with your interview preparation! π