319 challenges in the catalogue. Browse freely; sign in to open the editor and submit.
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
Example 1:
Input: nums = [1,2,3,1]
Output: true
Example 2:
Input: nums = [1,2,3,4]
Output: false
Example 3:
Input: nums = [1,1,1,3,3,4,3,2,4,2]
Output: true
1 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^9CREATE TABLE Employee (
id INT PRIMARY KEY,
name VARCHAR(255),
salary INT,
departmentId INT REFERENCES Department(id)
);
CREATE TABLE Department (
id INT PRIMARY KEY,
name VARCHAR(255)
);
Write a SQL query to find employees who have the highest salary in each of the departments.
Return the result table with columns Department, Employee, and Salary in any order.
If multiple employees have the same highest salary in a department, return all of them.
Input:
Employee table: | id | name | salary | departmentId | |----|-------|--------|--------------| | 1 | Joe | 70000 | 1 | | 2 | Jim | 90000 | 1 | | 3 | Henry | 80000 | 2 | | 4 | Sam | 60000 | 2 | | 5 | Max | 90000 | 1 |
Department table: | id | name | |----|-------| | 1 | IT | | 2 | Sales |
Output:
| Department | Employee | Salary | |------------|----------|--------| | IT | Jim | 90000 | | IT | Max | 90000 | | Sales | Henry | 80000 |
Explanation: Jim and Max both have the highest salary in IT. Henry has the highest salary in Sales.
Implement a simplified version of React's useEffect hook. Your implementation should:
function useEffect(effect: () => void | (() => void), deps?: any[]): void
effect — A function to run as a side effect. May return a cleanup function.deps (optional) — An array of dependencies. Effect re-runs when any dependency changes.| deps argument | When effect runs |
|-----------------|-----------------|
| Not provided | After every render |
| [] (empty) | Only after first render |
| [a, b] | When a or b change (shallow comparison) |
const { useState, useEffect, render } = createHooksSystem();
function Timer() {
const [count, setCount] = useState(0);
useEffect(() => {
console.log('Count changed:', count);
return () => console.log('Cleanup for:', count);
}, [count]);
return { count, increment: () => setCount(count + 1) };
}
function App() {
useEffect(() => {
console.log('Mounted');
return () => console.log('Unmounted');
}, []);
}
function Logger() {
useEffect(() => {
console.log('Rendered');
});
}
Object.is (shallow equality)Given a binary tree, determine if it is height-balanced.
A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than one.
Example 1:
Input: root = [3,9,20,null,null,15,7]
Output: true
Example 2:
Input: root = [1,2,2,3,3,null,null,4,4]
Output: false
Example 3:
Input: root = []
Output: true
[0, 5000].-10^4 <= Node.val <= 10^4Implement a promiseAll function that behaves like Promise.all. It takes an array of promises (or plain values) and returns a single promise that:
function promiseAll<T>(promises: Array<T | Promise<T>>): Promise<T[]>
promises — An array of promises or plain values.A promise that resolves to an array of resolved values in the same order as the input.
const result = await promiseAll([
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3)
]);
// [1, 2, 3]
const result = await promiseAll([1, 2, 3]);
// [1, 2, 3] (plain values are treated as resolved)
try {
await promiseAll([
Promise.resolve(1),
Promise.reject('error'),
Promise.resolve(3)
]);
} catch (e) {
console.log(e); // 'error'
}
const result = await promiseAll([]);
// [] (empty input resolves to empty array)
Promise.resolve(value)Given a string columnTitle that represents the column title as it appears in an Excel sheet, return its corresponding column number.
For example:
A -> 1B -> 2C -> 3Z -> 26AA -> 27AB -> 28Input: columnTitle = "A"
Output: 1
Input: columnTitle = "AB"
Output: 28
Input: columnTitle = "ZY"
Output: 701
1 <= columnTitle.length <= 7columnTitle consists only of uppercase English letters.columnTitle is in the range ["A", "FXSHRXW"].Given an input string s and a pattern p, implement regular expression matching with support for '.' and '*' where:
'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input string (not partial).
Example 1:
Input: s = "aa", p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".
Example 2:
Input: s = "aa", p = "a*"
Output: true
Explanation: '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
Example 3:
Input: s = "ab", p = ".*"
Output: true
Explanation: ".*" means "zero or more (*) of any character (.)".
1 <= s.length <= 201 <= p.length <= 20s contains only lowercase English letters.p contains only lowercase English letters, '.', and '*'.'*', there will be a previous valid character to match.You are given an m x n integer matrix matrix with the following two properties:
Given an integer target, return true if target is in matrix or false otherwise.
You must write a solution in O(log(m * n)) time complexity.
Example 1:
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
Output: true
Example 2:
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
Output: false
m == matrix.lengthn == matrix[i].length1 <= m, n <= 100-10^4 <= matrix[i][j] <= 10^4-10^4 <= target <= 10^4Given an array of intervals intervals where intervals[i] = [start_i, end_i], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
Example 1:
Input: intervals = [[1,2],[2,3],[3,4],[1,3]]
Output: 1
Explanation: [1,3] can be removed and the rest of the intervals are non-overlapping.
Example 2:
Input: intervals = [[1,2],[1,2],[1,2]]
Output: 2
Explanation: You need to remove two [1,2] intervals to make the rest non-overlapping.
Example 3:
Input: intervals = [[1,2],[2,3]]
Output: 0
Explanation: You don't need to remove any intervals since they are already non-overlapping.
1 <= intervals.length <= 10^5intervals[i].length == 2-5 * 10^4 <= start_i < end_i <= 5 * 10^4You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.
You are given an API isBadVersion(version) which returns whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.
Example 1:
Input: n = 5, bad = 4
Output: 4
Explanation:
isBadVersion(3) -> false
isBadVersion(5) -> true
isBadVersion(4) -> true
So 4 is the first bad version.
Example 2:
Input: n = 1, bad = 1
Output: 1
Example 3:
Input: n = 10, bad = 1
Output: 1
Explanation: All versions are bad. Version 1 is the first bad one.
1 <= bad <= n <= 2^31 - 1Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.
The overall run time complexity should be O(log (m+n)).
Example 1:
Input: nums1 = [1,3], nums2 = [2]
Output: 2.00000
Explanation: merged array = [1,2,3] and median is 2.
Example 2:
Input: nums1 = [1,2], nums2 = [3,4]
Output: 2.50000
Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
Example 3:
Input: nums1 = [], nums2 = [1]
Output: 1.00000
Explanation: Only one array has elements.
nums1.length == mnums2.length == n0 <= m <= 10000 <= n <= 10001 <= m + n <= 2000-10^6 <= nums1[i], nums2[i] <= 10^6Implement a deepClone function that creates a deep copy of a given value. The cloned value must have no shared references with the original — modifying the clone should never affect the original.
Your implementation should handle:
function deepClone<T>(obj: T): T
obj — Any JavaScript value to deep clone.A deep copy of the input value.
const original = { a: 1, b: { c: 2, d: [3, 4] } };
const cloned = deepClone(original);
cloned.b.c = 99;
console.log(original.b.c); // 2 (unaffected)
const arr = [1, [2, [3]]];
const clonedArr = deepClone(arr);
clonedArr[1][1][0] = 99;
console.log(arr[1][1][0]); // 3 (unaffected)
const withDate = { created: new Date('2024-01-01') };
const cloned = deepClone(withDate);
console.log(cloned.created instanceof Date); // true
console.log(cloned.created !== withDate.created); // true (different reference)
JSON.parse(JSON.stringify()) — it loses Date, RegExp, undefined, etc.