319 challenges in the catalogue. Browse freely; sign in to open the editor and submit.
CREATE TABLE Employee (
id INT PRIMARY KEY,
salary INT
);
Write a SQL query to find the Nth highest distinct salary from the Employee table. If there are fewer than N distinct salaries, return NULL.
For this problem, assume N = 2 in the examples, but your solution should work for any value of N.
Input:
Employee table: | id | salary | |----|--------| | 1 | 100 | | 2 | 200 | | 3 | 300 |
N = 2
Output:
| getNthHighestSalary(2) | |------------------------| | 200 |
Explanation: The 2nd highest distinct salary is 200.
Input:
Employee table: | id | salary | |----|--------| | 1 | 100 |
N = 2
Output:
| getNthHighestSalary(2) | |------------------------| | NULL |
Explanation: Only one distinct salary exists, so 2nd highest is NULL.
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.
Example 1:
Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
Output: [[1,0,1],[0,0,0],[1,0,1]]
Example 2:
Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
m == matrix.lengthn == matrix[0].length1 <= m, n <= 200-2^31 <= matrix[i][j] <= 2^31 - 1Design a stack-like data structure to push elements to the stack and pop the most frequent element from the stack.
Implement the FreqStack class:
FreqStack() constructs an empty frequency stack.push(val) pushes an integer val onto the top of the stack.pop() removes and returns the most frequent element in the stack. If there is a tie, the element closest to the stack's top is removed and returned.Example 1:
Input:
["FreqStack", "push", "push", "push", "push", "push", "push", "pop", "pop", "pop", "pop"]
[[], [5], [7], [5], [7], [4], [5], [], [], [], []]
Output: [null, null, null, null, null, null, null, 5, 7, 5, 4]
Explanation:
FreqStack freqStack = new FreqStack();
freqStack.push(5); // stack: [5]
freqStack.push(7); // stack: [5, 7]
freqStack.push(5); // stack: [5, 7, 5]
freqStack.push(7); // stack: [5, 7, 5, 7]
freqStack.push(4); // stack: [5, 7, 5, 7, 4]
freqStack.push(5); // stack: [5, 7, 5, 7, 4, 5]
freqStack.pop(); // return 5 (most frequent, appears 3 times)
freqStack.pop(); // return 7 (tied at freq 2, 7 is more recent)
freqStack.pop(); // return 5 (freq 2)
freqStack.pop(); // return 4 (freq 1, most recent among freq-1 elements)
0 <= val <= 10^92 * 10^4 calls will be made to push and pop.pop.There is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1 (inclusive). The edges in the graph are represented as a 2D integer array edges, where each edges[i] = [ui, vi] denotes a bi-directional edge between vertex ui and vertex vi. Every vertex pair is connected by at most one edge, and no vertex has an edge to itself.
You want to determine if there is a valid path that exists from vertex start to vertex end.
Given edges and the integers n, start, and end, return true if there is a valid path from start to end, or false otherwise.
Example 1:
Input: n = 3, edges = [[0,1],[1,2],[2,0]], start = 0, end = 2
Output: true
Explanation: There are two paths from vertex 0 to vertex 2:
- 0 -> 1 -> 2
- 0 -> 2
Example 2:
Input: n = 6, edges = [[0,1],[0,2],[3,5],[5,4],[4,3]], start = 0, end = 5
Output: false
Explanation: There is no path from vertex 0 to vertex 5.
1 <= n <= 2 * 10^50 <= edges.length <= 2 * 10^5edges[i].length == 20 <= ui, vi <= n - 1ui != vi0 <= start, end <= n - 1Design a scalable and low-latency News Feed system similar to Facebook's News Feed, Twitter's Timeline, or Instagram's Feed. A news feed is a constantly updating list of stories from people, pages, and groups that a user follows.
News feed is generated at the time of the request.
News feed is pre-computed and stored in a "feed cache" for each user.
Example Scenario:
Given a reference of a node in a connected undirected graph.
Return a 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;
}
Example 1:
Input: adjList = [[2,4],[1,3],[2,4],[1,3]]
Output: [[2,4],[1,3],[2,4],[1,3]]
Explanation: Node 1's neighbors are nodes 2 and 4. Node 2's neighbors are nodes 1 and 3. Node 3's neighbors are nodes 2 and 4. Node 4's neighbors are nodes 1 and 3.
Example 2:
Input: adjList = [[]]
Output: [[]]
Explanation: Node 1 has no neighbors.
Example 3:
Input: adjList = []
Output: []
Explanation: This means the graph is empty.
[0, 100].1 <= Node.val <= 100Node.val is unique for each node.Given an array of integers nums, your task is to answer multiple range minimum queries. Each query consists of two indices l and r (l <= r), and you must return the minimum value in the range nums[l...r].
To achieve the best performance for a static array (no updates), you should implement a Sparse Table. A Sparse Table allows you to answer these queries in O(1) time after an O(N log N) preprocessing step.
Input: nums = [7, 2, 3, 0, 5, 10, 3, 12, 18]
query(0, 4) -> Returns 0 (Min of [7, 2, 3, 0, 5])query(4, 7) -> Returns 3 (Min of [5, 10, 3, 12])query(7, 8) -> Returns 12 (Min of [12, 18])1 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^90 <= l <= r < nums.length10^5 queries.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.
Example 1:
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[7,4,1],[8,5,2],[9,6,3]]
Example 2:
Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
n == matrix.length == matrix[i].length1 <= n <= 20-1000 <= matrix[i][j] <= 1000Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target.
Return the sum of the three integers.
You may assume that each input would have exactly one solution.
Input: nums = [-1,2,1,-4], target = 1
Output: 2
Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
Input: nums = [0,0,0], target = 1
Output: 0
Explanation: The sum that is closest to the target is 0. (0 + 0 + 0 = 0).
3 <= nums.length <= 500-1000 <= nums[i] <= 1000-10^4 <= target <= 10^4You 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.
Return the max sliding window.
This challenge specifically focuses on implementing the solution using a Monotonic Queue to achieve $O(n)$ time complexity.
Input: nums = [1,3,-1,-3,5,3,6,7], k = 3
Output: [3,3,5,5,6,7]
Explanation:
Window position Max
[1 3 -1] -3 5 3 6 7 3 1 [3 -1 -3] 5 3 6 7 3 1 3 [-1 -3 5] 3 6 7 5 1 3 -1 [-3 5 3] 6 7 5 1 3 -1 -3 [5 3 6] 7 6 1 3 -1 -3 5 [3 6 7] 7
1 <= nums.length <= 10^5-10^4 <= nums[i] <= 10^41 <= k <= nums.lengthIn this problem, a tree is an undirected graph that is connected and has no cycles.
You are given a graph that started as a tree with n nodes labeled from 1 to n, with one additional edge added. The added edge has two different vertices chosen from 1 to n, and was not an edge that already existed. The graph is represented as an array edges of length n where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the graph.
Return an edge that can be removed so that the resulting graph is a tree of n nodes. If there are multiple answers, return the answer that occurs last in the input.
Example 1:
Input: edges = [[1,2],[1,3],[2,3]]
Output: [2,3]
Explanation: The connected nodes form a triangle. Removing [2,3] breaks the cycle.
Example 2:
Input: edges = [[1,2],[2,3],[3,4],[1,4],[1,5]]
Output: [1,4]
Explanation: The cycle is 1-2-3-4. Removing [1,4] breaks the cycle and is the last edge in the input causing/part of the cycle.
n == edges.length3 <= n <= 1000edges[i].length == 21 <= ai < bi <= edges.lengthai != biWrite a function that takes the binary representation of a positive integer n and returns the number of set bits it has (also known as the Hamming weight).
Input: n = 11
Output: 3
Explanation: The input binary string 1011 has a total of three set bits.
Input: n = 128
Output: 1
Explanation: The input binary string 10000000 has a total of one set bit.
1 <= n <= 2^31 - 1getFeed().