/LeetCode-HackerRank-MAANG

Solutions to LeetCode problems that can occur through interviews

Primary LanguagePython

Solutions to LeetCode & HackerRank problems.

Most common problems & solutions from MAANG interviews given.


license twitter


The repository is constantly updated when Python or SQL solution to the problem found at LeetCode or HackerRank is solved. The solutions to LeetCode problems are posted at the discussion sections in each solved problem folder. If the solved Leetcode problem is one of the problems asked by MAANG companies then that solution of specific company is added as my GitHub solution found through contents.

The progress of my LeetCode account regarding these solutions with additional information on them can be found by looking at my LeetCode profile.

The Leetcode solutions of Python are found here while HackerRank solutions of Python are found here. Each problem folder has the description of the problem found through pressing the link in README at the respective folder or if it is provided for the particular MAANG company found through contents. By pressing on that company you will be redirected to the problem description (1st column) followed by the solution in GitHub (2nd column).

Currently, doing Python problems of LeetCode. HackerRank problems were finished May and from 28th of May 2022 am working on solving at least one Python problem from LeetCode daily. The solution is put at the respective LeetCode folder and updated through MAANG contents list.

SQL solutions of problems at HackerRank & LeetCode will come in Fall while they will be updated here for HackerRank and here for LeetCode.

Table of Contents

Preparation for Interviews

To prepare for a MAANG interview or a MAANG-level company interview as well as land a job there I highly recommend solving as many problems below as possible and doing one or all of these:


MAANG Must Do Problems

No. Questions My GitHub Solution
1 Two Sum Two Sum
2 Merge Two Sorted Lists Merge Two Sorted Lists
3 Merge k Sorted Lists Merge k Sorted Lists
4 Best Time to Buy and Sell Stock Best Time to Buy and Sell Stock
5 Reverse Bits Reverse bits.
6 Number of 1 Bits Number of 1 bits
7 Contains Duplicate Contains Duplicate
8 Meeting Rooms Meeting Rooms.
9 Missing Number Missing Number
10 Counting Bits Counting bits
11 Merge Intervals Merge Intervals
12 Insert Interval Insert Interval
13 Valid Palindrome Valid Palindrome
14 Longest Substring Without Repeating Characters
15 Longest Palindromic Substring
16 Container With Most Water
17 3Sum
18 Remove Nth Node From End of List
19 Valid Parentheses
20 Search in Rotated Sorted Array
21 Combination Sum
22 Rotate Image
23 Group Anagrams
24 Maximum Subarray
25 Spiral Matrix
26 Jump Game
27 Unique Paths
28 Climbing Stairs
29 Set Matrix Zeroes
30 Minimum Window Substring
31 Word Search
32 Decode Ways
33 Validate Binary Search Tree
34 Same Tree
35 Binary Tree Level Order Traversal
36 Maximum Depth of Binary Tree
37 Construct Binary Tree from Preorder and Inorder Traversal
38 Binary Tree Maximum Path Sum
39 Longest Consecutive Sequence
40 Clone Graph
41 Word Break
42 Linked List Cycle
43 Reorder List
44 Maximum Product Subarray
45 Find Minimum in Rotated Sorted Array
46 House Robber
47 Number of Islands
48 Reverse Linked List
49 Course Schedule
50 Implement Trie (Prefix Tree)
51 Design Add and Search Words Data Structure
52 Word Search II
53 House Robber II
54 Invert Binary Tree
55 Kth Smallest Element in a BST
56 Lowest Common Ancestor of a Binary Search Tree
57 Lowest Common Ancestor of a Binary Tree
58 Product of Array Except Self
59 Valid Anagram
60 Meeting Rooms II
61 Graph Valid Tree
62 Alien Dictionary
63 Encode and Decode Strings
64 Find Median from Data Stream
65 Longest Increasing Subsequence
66 Coin Change
67 Number of Connected Components in an Undirected Graph
68 Top K Frequent Elements
69 Sum of Two Integers
70 Pacific Atlantic Water Flow
71 Longest Repeating Character Replacement
72 Non-overlapping Intervals
73 Serialize and Deserialize BST
74 Subtree of Another Tree
75 Palindromic Substrings
76 Longest Common Subsequence
77 Find the Duplicate Number

Meta

No. Questions My GitHub Solution
1 Merge k Sorted Lists Merge k Sorted Lists
2 Remove Duplicates from Sorted Array Remove Duplicates from Sorted Array
3 Interval List Intersections Interval List Intersections
4 Merge Sorted Array Merge Sorted Array
5 Best Time to Buy and Sell Stock Best Time to Buy and Sell Stock
6 Best Time to Buy and Sell Stock III Best Time to Buy and Sell Stock III
7 Best Time to Buy and Sell Stock IV Best Time to Buy and Sell Stock IV
8 Move Zeroes Move Zeroes
9 Roman to Integer Roman to Integer
10 Merge Intervals Merge Intervals
11 Insert Interval Insert Interval
12 Valid Palindrome II Valid Palindrome II
13 Regular Expression Matching
14 3Sum
15 Letter Combinations of a Phone Number
16 Valid Parentheses
17 Reverse Nodes in k-Group
18 Implement strStr()
19 Search in Rotated Sorted Array
20 Count and Say
21 Wildcard Matching
22 Pow(x, n)
23 Valid Palindrome
24 Longest Consecutive Sequence
25 Clone Graph
26 Word Break
27 LRU Cache
28 Binary Search Tree Iterator
29 Number of Islands
30 Implement Trie (Prefix Tree)
31 Kth Largest Element in an Array
32 The Skyline Problem
33 Maximal Square
34 Binary Tree Paths
35 H-Index
36 Expression Add Operators
37 Serialize and Deserialize Binary Tree
38 Flatten Nested List Iterator
39 Combination Sum IV
40 Insert Delete GetRandom O(1)
41 Target Sum
42 Encode and Decode TinyURL
43 Diameter of Binary Tree
44 Maximum Sum of 3 Non-Overlapping Subarrays
45 Sudoku Solver
46 Maximum Subarray
47 Valid Number
48 Plus One
49 Add Binary
50 Is Graph Bipartite?
51 Custom Sort String
52 Smallest Subtree with all the Deepest Nodes
53 Shortest Bridge
54 Range Sum of BST
55 Verifying an Alien Dictionary
56 K Closest Points to Origin
57 Max Consecutive Ones III
58 Minimum Remove to Make Valid Parentheses
59 Binary Tree Level Order Traversal
60 Binary Tree Maximum Path Sum
61 Fraction to Recurring Decimal
62 Simplify Path
63 Minimum Window Substring
64 Course Schedule
65 Design Add and Search Words Data Structure
66 Kth Smallest Element in a BST
67 Lowest Common Ancestor of a Binary Tree
68 Product of Array Except Self
69 Exclusive Time of Functions
70 First Bad Version
71 Task Scheduler
72 Remove Invalid Parentheses
73 Longest Increasing Path in a Matrix
74 Search in Rotated Sorted Array II
75 Convert a Number to Hexadecimal
76 Add Strings
77 Partition Equal Subset Sum
78 Find All Anagrams in a String
79 Continuous Subarray Sum
80 Subarray Sum Equals K

Amazon

No. Questions My GitHub Solution
1 Two Sum Two Sum
2 Add Two Numbers Add Two Numbers
3 String to Integer (atoi) String to Integer (atoi)
4 Merge Two Sorted Lists Merge Two Sorted Lists
5 Merge k Sorted Lists Merge k Sorted Lists
6 Permutations Permutations
7 Best Time to Buy and Sell Stock Best Time to Buy and Sell Stock
8 Merge Intervals Merge Intervals
9 Pascal's Triangle II Pascal's Triangle II
10 Longest Substring Without Repeating Characters
11 Median of Two Sorted Arrays
12 Longest Palindromic Substring
13 ZigZag Conversion
14 3Sum
15 Letter Combinations of a Phone Number
16 Valid Parentheses
17 Generate Parentheses
18 Trapping Rain Water
19 Rotate Image
20 Group Anagrams
21 Spiral Matrix II
22 Unique Paths II
23 Minimum Path Sum
24 Edit Distance
25 Set Matrix Zeroes
26 Sort Colors
27 Minimum Window Substring
28 Subsets
29 Gray Code
30 Validate Binary Search Tree
31 Same Tree
32 Binary Tree Level Order Traversal
33 Path Sum II
34 Word Ladder II
35 Word Ladder
36 Sum Root to Leaf Numbers
37 Copy List with Random Pointer
38 Word Break
39 Linked List Cycle
40 LRU Cache
41 Sort List
42 Min Stack
43 Intersection of Two Linked Lists
44 Two Sum II - Input array is sorted
45 Binary Search Tree Iterator
46 Rotate Array
47 Binary Tree Right Side View
48 Number of Islands
49 Happy Number
50 Count Primes
51 Reverse Linked List
52 Word Search II
53 Kth Largest Element in an Array
54 Basic Calculator
55 Palindrome Linked List
56 Lowest Common Ancestor of a Binary Search Tree
57 Lowest Common Ancestor of a Binary Tree
58 Product of Array Except Self
59 Sliding Window Maximum
60 Search a 2D Matrix II
61 Valid Anagram
62 Find Median from Data Stream
63 Serialize and Deserialize Binary Tree
64 Top K Frequent Elements
65 Design Twitter
66 Insert Delete GetRandom O(1)
67 Shuffle an Array
68 First Unique Character in a String
69 Rotate Function
70 Third Maximum Number
71 Battleships in a Board
72 Find All Anagrams in a String
73 String Compression
74 Serialize and Deserialize BST
75 Sort Characters By Frequency
76 Repeated Substring Pattern
77 LFU Cache
78 Concatenated Words
79 Most Frequent Subtree Sum
80 Longest Palindromic Subsequence
81 Super Washing Machines
82 Minesweeper
83 K-diff Pairs in an Array
84 Encode and Decode TinyURL
85 Complex Number Multiplication
86 Convert BST to Greater Tree
87 01 Matrix
88 Optimal Division
89 Subarray Sum Equals K
90 Subtree of Another Tree
91 Shortest Unsorted Continuous Subarray
92 Construct String from Binary Tree
93 Merge Two Binary Trees
94 Solve the Equation
95 Set Mismatch
96 Maximum Length of Pair Chain
97 Image Smoother
98 Maximum Width of Binary Tree
99 Cut Off Trees for Golf Event
100 Baseball Game
101 Top K Frequent Words
102 Accounts Merge
103 Find Pivot Index
104 Split Linked List in Parts
105 Monotone Increasing Digits
106 Min Cost Climbing Stairs
107 Prime Number of Set Bits in Binary Representation
108 Partition Labels
109 Reorganize String
110 Global and Local Inversions
111 Most Common Word
112 Unique Email Addresses
113 Knight Dialer
114 Reorder Data in Log Files
115 Prison Cells After N Days
116 K Closest Points to Origin
117 Subarrays with K Different Integers
118 Rotting Oranges
119 Binary Search Tree to Greater Sum Tree
120 Distant Barcodes
121 Sum of Nodes with Even-Valued Grandparent
122 Number of Dice Rolls With Target Sum
123 Critical Connections in a Network
124 Search Suggestions System
125 Number of Steps to Reduce a Number to Zero
126 Stone Game III

Google Top

No. Questions My GitHub Solution
1 Two Sum Two Sum
2 Maximal Rectangle Maximal Rectangle
3 Insert Interval Insert Interval
4 X of a Kind in a Deck of Cards X of a Kind in a Deck of Cards
5 Text Justification
6 Minimum Window Substring
7 The Skyline Problem
8 Maximal Square
9 Find Median from Data Stream
10 Bulls and Cows
11 Count of Smaller Numbers After Self
12 Longest Increasing Path in a Matrix
13 Max Sum of Rectangle No Larger Than K
14 Decode String
15 Evaluate Division
16 Split Array Largest Sum
17 Subarray Sum Equals K
18 Split Array into Consecutive Subsequences
19 24 Game
20 Network Delay Time
21 Open the Lock
22 Expressive Words
23 Find And Replace in String
24 Guess the Word
25 Hand of Straights
26 Shortest Subarray with Sum at Least K
27 Random Pick with Weight
28 Minimum Area Rectangle
29 Validate Stack Sequences
30 Flip Equivalent Binary Trees
31 Minimum Domino Rotations For Equal Row
32 Longest String Chain
33 Delete Nodes And Return Forest
34 Snapshot Array
35 Divide Array in Sets of K Consecutive Numbers
36 Minimum Distance to Type a Word Using Two Fingers
37 Time Needed to Inform All Employees

Google

No. Questions My GitHub Solution
1 Two Sum Two Sum
2 Merge k Sorted Lists Merge k Sorted Lists
3 Remove Duplicates from Sorted Array Remove Duplicates from Sorted Array
4 Next Permutation Next Permutation
5 Maximal Rectangle Maximal Rectangle
6 Move Zeroes Move Zeroes
7 Merge Intervals Merge Intervals
8 Insert Interval Insert Interval
9 Pascal's Triangle Pascal's Triangle
10 Median of Two Sorted Arrays
11 Regular Expression Matching
12 Letter Combinations of a Phone Number
13 Valid Parentheses
14 Generate Parentheses
15 Trapping Rain Water
16 Wildcard Matching
17 Pow(x, n)
18 Spiral Matrix
19 Plus One
20 Same Tree
21 Word Ladder
22 Longest Consecutive Sequence
23 Clone Graph
24 Word Break
25 Word Break II
26 LRU Cache
27 Min Stack
28 Find Peak Element
29 Fraction to Recurring Decimal
30 Binary Search Tree Iterator
31 House Robber
32 Number of Islands
33 Isomorphic Strings
34 Implement Trie (Prefix Tree)
35 Word Search II
36 Shortest Palindrome
37 The Skyline Problem
38 Count Complete Tree Nodes
39 Basic Calculator
40 Summary Ranges
41 Kth Smallest Element in a BST
42 Power of Two
43 Product of Array Except Self
44 Sliding Window Maximum
45 Search a 2D Matrix II
46 Binary Tree Paths
47 H-Index
48 Perfect Squares
49 Expression Add Operators
50 Peeking Iterator
51 Game of Life
52 Find Median from Data Stream
53 Serialize and Deserialize Binary Tree
54 Bulls and Cows
55 Longest Increasing Subsequence
56 Best Time to Buy and Sell Stock with Cooldown
57 Minimum Height Trees
58 Burst Balloons
59 Super Ugly Number
60 Count of Smaller Numbers After Self
61 Remove Duplicate Letters
62 Maximum Product of Word Lengths
63 Create Maximum Number
64 Wiggle Sort II
65 Power of Three
66 Count of Range Sum
67 Longest Increasing Path in a Matrix
68 Patching Array
69 Verify Preorder Serialization of a Binary Tree
70 Reconstruct Itinerary
71 Palindrome Pairs
72 Flatten Nested List Iterator
73 Reverse Vowels of a String
74 Russian Doll Envelopes
75 Count Numbers with Unique Digits
76 Max Sum of Rectangle No Larger Than K
77 Find K Pairs with Smallest Sums
78 Guess Number Higher or Lower
79 Guess Number Higher or Lower II
80 Combination Sum IV
81 Kth Smallest Element in a Sorted Matrix
82 Insert Delete GetRandom O(1)
83 Linked List Random Node
84 First Unique Character in a String
85 Longest Absolute File Path
86 Find the Difference
87 Perfect Rectangle
88 UTF-8 Validation
89 Decode String
90 Integer Replacement
91 Evaluate Division
92 Nth Digit
93 Binary Watch
94 Remove K Digits
95 Queue Reconstruction by Height
96 Trapping Rain Water II
97 Longest Palindrome
98 Split Array Largest Sum
99 Add Strings
100 Pacific Atlantic Water Flow
101 Maximum XOR of Two Numbers in an Array
102 Number of Boomerangs
103 Find All Numbers Disappeared in an Array
104 Sort Characters By Frequency
105 Repeated Substring Pattern
106 LFU Cache
107 Island Perimeter
108 Ones and Zeroes
109 Heaters
110 Sliding Window Median
111 Magical String
112 License Key Formatting
113 Smallest Good Base
114 Max Consecutive Ones
115 Predict the Winner
116 Reverse Pairs
117 Target Sum
118 Diagonal Traverse
119 Find Mode in Binary Search Tree
120 Next Greater Element II
121 Relative Ranks
122 Freedom Trail
123 Longest Palindromic Subsequence
124 Detect Capital
125 Longest Uncommon Subsequence I
126 Longest Uncommon Subsequence II
127 Longest Word in Dictionary through Deleting
128 Beautiful Arrangement
129 Minimum Absolute Difference in BST
130 Encode and Decode TinyURL
131 Reverse String II
132 01 Matrix
133 Diameter of Binary Tree
134 Student Attendance Record I
135 Student Attendance Record II
136 Subarray Sum Equals K
137 Shortest Unsorted Continuous Subarray
138 Delete Operation for Two Strings
139 Erect the Fence
140 Smallest Range Covering Elements from K Lists
141 Shopping Offers
142 Maximum Average Subarray I
143 Find Duplicate Subtrees
144 Find K Closest Elements
145 Split Array into Consecutive Subsequences
146 Non-decreasing Array
147 Beautiful Arrangement II
148 Kth Smallest Number in Multiplication Table
149 Implement Magic Dictionary
150 Valid Parenthesis String
151 24 Game
152 Redundant Connection
153 Redundant Connection II
154 Repeated String Match
155 Longest Univalue Path
156 Maximum Sum of 3 Non-Overlapping Subarrays
157 Find K-th Smallest Pair Distance
158 Number of Atoms
159 My Calendar I
160 My Calendar II
161 My Calendar III
162 Daily Temperatures
163 Largest Number At Least Twice of Others
164 Shortest Completing Word
165 Open the Lock
166 Cracking the Safe
167 Pyramid Transition Matrix
168 Couples Holding Hands
169 Toeplitz Matrix
170 Number of Matching Subsequences
171 Minimum Swaps To Make Sequences Increasing
172 Backspace String Compare
173 Maximize Distance to Closest Person
174 All Possible Full Binary Trees
175 Fruit Into Baskets
176 Long Pressed Name
177 Unique Email Addresses
178 Knight Dialer
179 Minimum Area Rectangle
180 K Closest Points to Origin
181 Odd Even Jump
182 Shortest Way to Form String
183 Campus Bikes
184 Confusing Number II
185 Video Stitching
186 Stream of Characters
187 Maximum Level Sum of a Binary Tree
188 String Transforms Into Another String
189 Number of Submatrices That Sum to Target
190 Divide Chocolate
191 Delete Nodes And Return Forest
192 Compare Strings by Frequency of the Smallest Character
193 Tiling a Rectangle with the Fewest Squares
194 Count Number of Nice Subarrays

Microsoft

No. Questions My GitHub Solution
1 Two Sum Two Sum
2 Add Two Numbers Add Two Numbers
3 String to Integer (atoi) String to Integer (atoi)
4 Roman to Integer Roman to Integer
5 Merge Two Sorted Lists Merge Two Sorted Lists
6 Merge k Sorted Lists Merge k Sorted Lists
7 Number of 1 Bits Number of 1 bits
8 Missing Number Missing Number
9 Remove Duplicates from Sorted Array Remove Duplicates from Sorted Array
10 Merge Intervals Merge Intervals
11 Valid Palindrome Valid Palindrome
12 Median of Two Sorted Arrays
13 Longest Palindromic Substring
14 3Sum
15 Valid Parentheses
16 Swap Nodes in Pairs
17 Reverse Nodes in k-Group
18 Implement strStr()
19 Search in Rotated Sorted Array
20 Maximum Subarray
21 Spiral Matrix
22 Jump Game
23 Binary Tree Maximum Path Sum
24 Copy List with Random Pointer
25 Linked List Cycle
26 LRU Cache
27 Reverse Words in a String
28 Find Minimum in Rotated Sorted Array
29 Find Peak Element
30 Excel Sheet Column Title
31 Excel Sheet Column Number
32 Binary Search Tree Iterator
33 Rotate Array
34 Number of Islands
35 Reverse Linked List
36 Implement Trie (Prefix Tree)
37 Word Search II
38 Kth Largest Element in an Array
39 The Skyline Problem
40 Delete Node in a Linked List
41 Product of Array Except Self
42 Add Digits
43 Serialize and Deserialize Binary Tree
44 First Unique Character in a String
45 Arithmetic Slices
46 String Compression
47 Max Chunks To Make Sorted
48 Valid Tic-Tac-Toe State
49 Maximum Length of a Concatenated String with Unique Characters
50 Find N Unique Integers Sum up to Zero
51 Jump Game III
52 Cinema Seat Allocation
53 Bulb Switcher III

LinkedIn

No. Questions My GitHub Solution
1 Nested List Weight Sum II
2 Shortest Word Distance II
3 Closest Binary Search Tree Value II
4 Two Sum III - Data structure design
5 Nested List Weight Sum
6 Max Stack
7 Find Leaves of Binary Tree
8 All O'one Data Structure
9 Can Place Flowers
10 Factor Combinations
11 Paint House
12 Paint House II
13 Evaluate Reverse Polish Notation
14 Shortest Word Distance
15 Text Justification
16 Count Different Palindromic Subsequences
17 Binary Tree Upside Down
18 Max Points on a Line
19 Partition to K Equal Sum Subsets
20 Insert Delete GetRandom O(1)
21 Number of Islands
22 Exclusive Time of Functions
23 Valid Triangle Number
24 Valid Number
25 Repeated DNA Sequences

Netflix

No. Questions My GitHub Solution
1 LRU Cache
2 Department Top Three Salaries