Gallery
LeetCode Question Generator
Share
Explore
LeetCode Question Generator
Questions

🔎 Browse questions

Hide completed questions:
Array
11
Easy
4
Two Sum
Best Time to Buy and Sell Stock
Contains Duplicate
Majority Element
Medium
7
Product of Array Except Self
Maximum Product Subarray
Find Minimum in Rotated Sorted Array
3 Sum
Container With Most Water
3Sum
Sort Colors
Binary
6
Easy
5
Number of 1 Bits
Counting Bits
Missing Number
Reverse Bits
Add Binary
Medium
1
Sum of Two Integers
Binary Search
5
Easy
2
Binary Search
First Bad Version
Medium
2
Search in Rotated Sorted Array
Time Based Key-Value Store
Hard
1
Maximum Profit in Job Scheduling
Binary Search Tree
4
Easy
2
Maximum Depth of Binary Tree
Lowest Common Ancestor of a Binary Search Tree
Medium
2
Validate Binary Search Tree
Kth Smallest Element in a BST
Binary Tree
8
Easy
3
Invert Binary Tree
Balanced Binary Tree
Diameter of Binary Tree
Medium
4
Binary Tree Level Order Traversal
Construct Binary Tree from Preorder and Inorder Traversal
Lowest Common Ancestor of a Binary Tree
Binary Tree Right Side View
Hard
1
Serialize and Deserialize Binary Tree
Dynamic Programming
13
Easy
2
Maximum Subarray
Climbing Stairs
Medium
11
Coin Change
Longest Increasing Subsequence
Longest Common Subsequence
Word Break Problem
Combination Sum
House Robber
House Robber II
Decode Ways
Unique Paths
Jump Game
Partition Equal Subset Sum
Graph
15
Easy
1
Flood Fill
Medium
12
Clone Graph
Course Schedule
Pacific Atlantic Water Flow
Number of Islands
Longest Consecutive Sequence
Graph Valid Tree (Leetcode Premium)
Number of Connected Components in an Undirected Graph (Leetcode Premium)
Word Search
01 Matrix
Rotting Oranges
Accounts Merge
Minimum Height Trees
Hard
2
Alien Dictionary (Leetcode Premium)
Word Ladder
Hash Table
1
Easy
1
Ransom Note
Heap
4
Medium
3
Top K Frequent Elements
K Closest Points to Origin
Task Scheduler
Hard
1
Find Median from Data Stream
Interval
5
Easy
1
Meeting Rooms (Leetcode Premium)
Medium
4
Insert Interval
Merge Intervals
Non-overlapping Intervals
Meeting Rooms II (Leetcode Premium)
Linked List
10
Easy
6
Reverse a Linked List
Detect Cycle in a Linked List
Merge Two Sorted Lists
Linked List Cycle
Reverse Linked List
Middle of the Linked List
Medium
3
Remove Nth Node From End Of List
Reorder List
LRU Cache
Hard
1
Merge K Sorted Lists
Matrix
3
Medium
3
Set Matrix Zeroes
Spiral Matrix
Rotate Image
Recursion
3
Medium
3
Permutations
Subsets
Letter Combinations of a Phone Number
Stack
7
Easy
3
Valid Parentheses
Implement Queue using Stacks
Min Stack
Medium
1
Evaluate Reverse Polish Notation
Hard
3
Trapping Rain Water
Basic Calculator
Largest Rectangle in Histogram
String
12
Easy
3
Valid Anagram
Valid Palindrome
Longest Palindrome
Medium
8
Longest Substring Without Repeating Characters
Longest Repeating Character Replacement
Group Anagrams
Longest Palindromic Substring
Palindromic Substrings
Encode and Decode Strings (Leetcode Premium)
String to Integer (atoi)
Find All Anagrams in a String
Hard
1
Minimum Window Substring
Tree
8
Easy
4
Same Tree
Invert/Flip Binary Tree
Subtree of Another Tree
Lowest Common Ancestor of BST
Medium
2
Implement Trie (Prefix Tree)
Add and Search Word
Hard
2
Binary Tree Maximum Path Sum
Word Search II
Trie
1
Medium
1
Word Break


View of _Questions
6
Category
Name
Difficulty
Link
BrowseLink
Premium
Done?
Difficulty Value
Mark Done
Completed Time
8
Binary
Reverse Bits
Easy
1
Mark done
26
Graph
Flood Fill
Easy
Flood Fill - LeetCode
Can you solve this real interview question? Flood Fill - You are given an image represented by an m x n grid of integers image, where image[i][j] represents the pixel value of the image. You are also given three integers sr, sc, and color. Your task is to perform a flood fill on the image starting from the pixel image[sr][sc]. To perform a flood fill: 1. Begin with the starting pixel and change its color to color. 2. Perform the same process for each pixel that is directly adjacent (pixels that share a side with the original pixel, either horizontally or vertically) and shares the same color as the starting pixel. 3. Keep repeating this process by checking neighboring pixels of the updated pixels and modifying their color if it matches the original color of the starting pixel. 4. The process stops when there are no more adjacent pixels of the original color to update. Return the modified image after performing the flood fill.   Example 1: Input: image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, color = 2 Output: [[2,2,2],[2,2,0],[2,0,1]] Explanation: [https://assets.leetcode.com/uploads/2021/06/01/flood1-grid.jpg] From the center of the image with position (sr, sc) = (1, 1) (i.e., the red pixel), all pixels connected by a path of the same color as the starting pixel (i.e., the blue pixels) are colored with the new color. Note the bottom corner is not colored 2, because it is not horizontally or vertically connected to the starting pixel. Example 2: Input: image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, color = 0 Output: [[0,0,0],[0,0,0]] Explanation: The starting pixel is already colored with 0, which is the same as the target color. Therefore, no changes are made to the image.   Constraints: * m == image.length * n == image[i].length * 1 <= m, n <= 50 * 0 <= image[i][j], color < 216 * 0 <= sr < m * 0 <= sc < n
leetcode.com
1
Mark done
35
Stack
Implement Queue using Stacks
Easy
Implement Queue using Stacks - LeetCode
Can you solve this real interview question? Implement Queue using Stacks - Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty). Implement the MyQueue class: * void push(int x) Pushes element x to the back of the queue. * int pop() Removes the element from the front of the queue and returns it. * int peek() Returns the element at the front of the queue. * boolean empty() Returns true if the queue is empty, false otherwise. Notes: * You must use only standard operations of a stack, which means only push to top, peek/pop from top, size, and is empty operations are valid. * Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque (double-ended queue) as long as you use only a stack's standard operations.   Example 1: Input ["MyQueue", "push", "push", "peek", "pop", "empty"] [[], [1], [2], [], [], []] Output [null, null, null, 1, 1, false] Explanation MyQueue myQueue = new MyQueue(); myQueue.push(1); // queue is: [1] myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue) myQueue.peek(); // return 1 myQueue.pop(); // return 1, queue is [2] myQueue.empty(); // return false   Constraints: * 1 <= x <= 9 * At most 100 calls will be made to push, pop, peek, and empty. * All the calls to pop and peek are valid.   Follow-up: Can you implement the queue such that each operation is amortized [https://en.wikipedia.org/wiki/Amortized_analysis] O(1) time complexity? In other words, performing n operations will take overall O(n) time even if one of those operations may take longer.
leetcode.com
1
Mark done
52
Dynamic Programming
Decode Ways
Medium
2
Mark done
55
Graph
Clone Graph
Medium
Clone Graph - LeetCode
Can you solve this real interview question? Clone Graph - Given a reference of a node in a connected [https://en.wikipedia.org/wiki/Connectivity_(graph_theory)#Connected_graph] undirected graph. Return a deep copy [https://en.wikipedia.org/wiki/Object_copying#Deep_copy] (clone) of the graph. Each node in the graph contains a value (int) and a list (List[Node]) of its neighbors. class Node { public int val; public List<Node> neighbors; }   Test case format: For simplicity, each node's value is the same as the node's index (1-indexed). For example, the first node with val == 1, the second node with val == 2, and so on. The graph is represented in the test case using an adjacency list. An adjacency list is a collection of unordered lists used to represent a finite graph. Each list describes the set of neighbors of a node in the graph. The given node will always be the first node with val = 1. You must return the copy of the given node as a reference to the cloned graph.   Example 1: [https://assets.leetcode.com/uploads/2019/11/04/133_clone_graph_question.png] Input: adjList = [[2,4],[1,3],[2,4],[1,3]] Output: [[2,4],[1,3],[2,4],[1,3]] Explanation: There are 4 nodes in the graph. 1st node (val = 1)'s neighbors are 2nd node (val = 2) and 4th node (val = 4). 2nd node (val = 2)'s neighbors are 1st node (val = 1) and 3rd node (val = 3). 3rd node (val = 3)'s neighbors are 2nd node (val = 2) and 4th node (val = 4). 4th node (val = 4)'s neighbors are 1st node (val = 1) and 3rd node (val = 3). Example 2: [https://assets.leetcode.com/uploads/2020/01/07/graph.png] Input: adjList = [[]] Output: [[]] Explanation: Note that the input contains one empty list. The graph consists of only one node with val = 1 and it does not have any neighbors. Example 3: Input: adjList = [] Output: [] Explanation: This an empty graph, it does not have any nodes.   Constraints: * The number of nodes in the graph is in the range [0, 100]. * 1 <= Node.val <= 100 * Node.val is unique for each node. * There are no repeated edges and no self-loops in the graph. * The Graph is connected and all nodes can be visited starting from the given node.
leetcode.com
2
Mark done
57
Graph
Pacific Atlantic Water Flow
Medium
Pacific Atlantic Water Flow - LeetCode
Can you solve this real interview question? Pacific Atlantic Water Flow - There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island's left and top edges, and the Atlantic Ocean touches the island's right and bottom edges. The island is partitioned into a grid of square cells. You are given an m x n integer matrix heights where heights[r][c] represents the height above sea level of the cell at coordinate (r, c). The island receives a lot of rain, and the rain water can flow to neighboring cells directly north, south, east, and west if the neighboring cell's height is less than or equal to the current cell's height. Water can flow from any cell adjacent to an ocean into the ocean. Return a 2D list of grid coordinates result where result[i] = [ri, ci] denotes that rain water can flow from cell (ri, ci) to both the Pacific and Atlantic oceans.   Example 1: [https://assets.leetcode.com/uploads/2021/06/08/waterflow-grid.jpg] Input: heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]] Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]] Explanation: The following cells can flow to the Pacific and Atlantic oceans, as shown below: [0,4]: [0,4] -> Pacific Ocean   [0,4] -> Atlantic Ocean [1,3]: [1,3] -> [0,3] -> Pacific Ocean   [1,3] -> [1,4] -> Atlantic Ocean [1,4]: [1,4] -> [1,3] -> [0,3] -> Pacific Ocean   [1,4] -> Atlantic Ocean [2,2]: [2,2] -> [1,2] -> [0,2] -> Pacific Ocean   [2,2] -> [2,3] -> [2,4] -> Atlantic Ocean [3,0]: [3,0] -> Pacific Ocean   [3,0] -> [4,0] -> Atlantic Ocean [3,1]: [3,1] -> [3,0] -> Pacific Ocean   [3,1] -> [4,1] -> Atlantic Ocean [4,0]: [4,0] -> Pacific Ocean [4,0] -> Atlantic Ocean Note that there are other possible paths for these cells to flow to the Pacific and Atlantic oceans. Example 2: Input: heights = [[1]] Output: [[0,0]] Explanation: The water can flow from the only cell to the Pacific and Atlantic oceans.   Constraints: * m == heights.length * n == heights[r].length * 1 <= m, n <= 200 * 0 <= heights[r][c] <= 105
leetcode.com
2
Mark done
87
String
String to Integer (atoi)
Medium
String to Integer (atoi) - LeetCode
Can you solve this real interview question? String to Integer (atoi) - Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer. The algorithm for myAtoi(string s) is as follows: 1. Whitespace: Ignore any leading whitespace (" "). 2. Signedness: Determine the sign by checking if the next character is '-' or '+', assuming positivity is neither present. 3. Conversion: Read the integer by skipping leading zeros until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0. 4. Rounding: If the integer is out of the 32-bit signed integer range [-231, 231 - 1], then round the integer to remain in the range. Specifically, integers less than -231 should be rounded to -231, and integers greater than 231 - 1 should be rounded to 231 - 1. Return the integer as the final result.   Example 1: Input: s = "42" Output: 42 Explanation: The underlined characters are what is read in and the caret is the current reader position. Step 1: "42" (no characters read because there is no leading whitespace) ^ Step 2: "42" (no characters read because there is neither a '-' nor '+') ^ Step 3: "42" ("42" is read in) ^ Example 2: Input: s = " -042" Output: -42 Explanation: Step 1: " -042" (leading whitespace is read and ignored) ^ Step 2: " -042" ('-' is read, so the result should be negative) ^ Step 3: " -042" ("042" is read in, leading zeros ignored in the result) ^ Example 3: Input: s = "1337c0d3" Output: 1337 Explanation: Step 1: "1337c0d3" (no characters read because there is no leading whitespace) ^ Step 2: "1337c0d3" (no characters read because there is neither a '-' nor '+') ^ Step 3: "1337c0d3" ("1337" is read in; reading stops because the next character is a non-digit) ^ Example 4: Input: s = "0-1" Output: 0 Explanation: Step 1: "0-1" (no characters read because there is no leading whitespace) ^ Step 2: "0-1" (no characters read because there is neither a '-' nor '+') ^ Step 3: "0-1" ("0" is read in; reading stops because the next character is a non-digit) ^ Example 5: Input: s = "words and 987" Output: 0 Explanation: Reading stops at the first non-digit character 'w'.   Constraints: * 0 <= s.length <= 200 * s consists of English letters (lower-case and upper-case), digits (0-9), ' ', '+', '-', and '.'.
leetcode.com
2
Mark done
89
Binary Search
Time Based Key-Value Store
Medium
Time Based Key-Value Store - LeetCode
Can you solve this real interview question? Time Based Key-Value Store - Design a time-based key-value data structure that can store multiple values for the same key at different time stamps and retrieve the key's value at a certain timestamp. Implement the TimeMap class: * TimeMap() Initializes the object of the data structure. * void set(String key, String value, int timestamp) Stores the key key with the value value at the given time timestamp. * String get(String key, int timestamp) Returns a value such that set was called previously, with timestamp_prev <= timestamp. If there are multiple such values, it returns the value associated with the largest timestamp_prev. If there are no values, it returns "".   Example 1: Input ["TimeMap", "set", "get", "get", "set", "get", "get"] [[], ["foo", "bar", 1], ["foo", 1], ["foo", 3], ["foo", "bar2", 4], ["foo", 4], ["foo", 5]] Output [null, null, "bar", "bar", null, "bar2", "bar2"] Explanation TimeMap timeMap = new TimeMap(); timeMap.set("foo", "bar", 1); // store the key "foo" and value "bar" along with timestamp = 1. timeMap.get("foo", 1); // return "bar" timeMap.get("foo", 3); // return "bar", since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 is "bar". timeMap.set("foo", "bar2", 4); // store the key "foo" and value "bar2" along with timestamp = 4. timeMap.get("foo", 4); // return "bar2" timeMap.get("foo", 5); // return "bar2"   Constraints: * 1 <= key.length, value.length <= 100 * key and value consist of lowercase English letters and digits. * 1 <= timestamp <= 107 * All the timestamps timestamp of set are strictly increasing. * At most 2 * 105 calls will be made to set and get.
leetcode.com
2
Mark done
92
Graph
Accounts Merge
Medium
Accounts Merge - LeetCode
Can you solve this real interview question? Accounts Merge - Given a list of accounts where each element accounts[i] is a list of strings, where the first element accounts[i][0] is a name, and the rest of the elements are emails representing emails of the account. Now, we would like to merge these accounts. Two accounts definitely belong to the same person if there is some common email to both accounts. Note that even if two accounts have the same name, they may belong to different people as people could have the same name. A person can have any number of accounts initially, but all of their accounts definitely have the same name. After merging the accounts, return the accounts in the following format: the first element of each account is the name, and the rest of the elements are emails in sorted order. The accounts themselves can be returned in any order.   Example 1: Input: accounts = [["John","johnsmith@mail.com","john_newyork@mail.com"],["John","johnsmith@mail.com","john00@mail.com"],["Mary","mary@mail.com"],["John","johnnybravo@mail.com"]] Output: [["John","john00@mail.com","john_newyork@mail.com","johnsmith@mail.com"],["Mary","mary@mail.com"],["John","johnnybravo@mail.com"]] Explanation: The first and second John's are the same person as they have the common email "johnsmith@mail.com". The third John and Mary are different people as none of their email addresses are used by other accounts. We could return these lists in any order, for example the answer [['Mary', 'mary@mail.com'], ['John', 'johnnybravo@mail.com'], ['John', 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com']] would still be accepted. Example 2: Input: accounts = [["Gabe","Gabe0@m.co","Gabe3@m.co","Gabe1@m.co"],["Kevin","Kevin3@m.co","Kevin5@m.co","Kevin0@m.co"],["Ethan","Ethan5@m.co","Ethan4@m.co","Ethan0@m.co"],["Hanzo","Hanzo3@m.co","Hanzo1@m.co","Hanzo0@m.co"],["Fern","Fern5@m.co","Fern1@m.co","Fern0@m.co"]] Output: [["Ethan","Ethan0@m.co","Ethan4@m.co","Ethan5@m.co"],["Gabe","Gabe0@m.co","Gabe1@m.co","Gabe3@m.co"],["Hanzo","Hanzo0@m.co","Hanzo1@m.co","Hanzo3@m.co"],["Kevin","Kevin0@m.co","Kevin3@m.co","Kevin5@m.co"],["Fern","Fern0@m.co","Fern1@m.co","Fern5@m.co"]]   Constraints: * 1 <= accounts.length <= 1000 * 2 <= accounts[i].length <= 10 * 1 <= accounts[i][j].length <= 30 * accounts[i][0] consists of English letters. * accounts[i][j] (for j > 0) is a valid email.
leetcode.com
2
Mark done
93
Graph
Minimum Height Trees
Medium
2
Mark done
99
Linked List
LRU Cache
Medium
LRU Cache - LeetCode
Can you solve this real interview question? LRU Cache - Design a data structure that follows the constraints of a Least Recently Used (LRU) cache [https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU]. Implement the LRUCache class: * LRUCache(int capacity) Initialize the LRU cache with positive size capacity. * int get(int key) Return the value of the key if the key exists, otherwise return -1. * void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recently used key. The functions get and put must each run in O(1) average time complexity.   Example 1: Input ["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"] [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]] Output [null, null, null, 1, null, -1, null, -1, 3, 4] Explanation LRUCache lRUCache = new LRUCache(2); lRUCache.put(1, 1); // cache is {1=1} lRUCache.put(2, 2); // cache is {1=1, 2=2} lRUCache.get(1); // return 1 lRUCache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3} lRUCache.get(2); // returns -1 (not found) lRUCache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3} lRUCache.get(1); // return -1 (not found) lRUCache.get(3); // return 3 lRUCache.get(4); // return 4   Constraints: * 1 <= capacity <= 3000 * 0 <= key <= 104 * 0 <= value <= 105 * At most 2 * 105 calls will be made to get and put.
leetcode.com
2
Mark done
102
Heap
Task Scheduler
Medium
2
Mark done
111
Heap
Find Median from Data Stream
Hard
Find Median from Data Stream - LeetCode
Can you solve this real interview question? Find Median from Data Stream - 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. * For example, for arr = [2,3,4], the median is 3. * For example, for arr = [2,3], the median is (2 + 3) / 2 = 2.5. Implement the MedianFinder class: * MedianFinder() initializes the MedianFinder object. * void addNum(int num) adds the integer num from the data stream to the data structure. * double findMedian() returns the median of all elements so far. Answers within 10-5 of the actual answer will be accepted.   Example 1: Input ["MedianFinder", "addNum", "addNum", "findMedian", "addNum", "findMedian"] [[], [1], [2], [], [3], []] Output [null, null, null, 1.5, null, 2.0] Explanation MedianFinder medianFinder = new MedianFinder(); medianFinder.addNum(1); // arr = [1] medianFinder.addNum(2); // arr = [1, 2] medianFinder.findMedian(); // return 1.5 (i.e., (1 + 2) / 2) medianFinder.addNum(3); // arr[1, 2, 3] medianFinder.findMedian(); // return 2.0   Constraints: * -105 <= num <= 105 * There will be at least one element in the data structure before calling findMedian. * At most 5 * 104 calls will be made to addNum and findMedian.   Follow up: * If all integer numbers from the stream are in the range [0, 100], how would you optimize your solution? * If 99% of all integer numbers from the stream are in the range [0, 100], how would you optimize your solution?
leetcode.com
3
Mark done
There are no rows in this table

Share
 
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.