319 challenges in the catalogue. Browse freely; sign in to open the editor and submit.
Given a character array tasks, representing the tasks a CPU needs to do, where each letter represents a different task. Tasks could be done in any order. Each task is done in one unit of time. For each unit of time, the CPU could complete either one task or just be idle.
However, there is a non-negative integer n that represents the cooldown period between two same tasks (the same letter in the array), that is, there must be at least n units of time between any two same tasks.
Return the least number of units of times that the CPU will take to finish all the given tasks.
Example 1:
Input: tasks = ["A","A","A","B","B","B"], n = 2
Output: 8
Explanation:
A -> B -> idle -> A -> B -> idle -> A -> B
There is at least 2 units of time between any two same tasks.
Example 2:
Input: tasks = ["A","A","A","B","B","B"], n = 0
Output: 6
Explanation: On this case any permutation of size 6 would work since n = 0.
["A","A","A","B","B","B"]
["A","B","A","B","A","B"]
["B","B","B","A","A","A"]
...
And so on.
Example 3:
Input: tasks = ["A","A","A","A","A","A","B","C","D","E","F","G"], n = 2
Output: 16
Explanation:
One possible solution is
A -> B -> C -> A -> D -> E -> A -> F -> G -> A -> idle -> idle -> A -> idle -> idle -> A
1 <= task.length <= 10^4tasks[i] is upper-case English letter.0 <= n <= 100Given an array of distinct integers nums and a target integer target, return the number of possible combinations that add up to target.
The test cases are generated so that the answer can fit in a 32-bit integer.
Note that different sequences are counted as different combinations. For example, [1, 2, 1] and [2, 1, 1] are different combinations.
Example 1:
Input: nums = [1,2,3], target = 4
Output: 7
Explanation:
The possible combination ways are:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1)
Note that different sequences are counted as different combinations.
Example 2:
Input: nums = [9], target = 3
Output: 0
Explanation: No combination can sum to 3 using only 9.
1 <= nums.length <= 2001 <= nums[i] <= 1000nums are unique.1 <= target <= 1000What if negative numbers are allowed in the given array? How does it change the problem?
Cross-Site Scripting (XSS) is a type of security vulnerability where an attacker injects malicious scripts into content from otherwise trusted websites.
One of the primary ways to prevent XSS is to sanitize user-provided data before rendering it in the browser. This usually involves "escaping" special characters that have meaning in HTML.
Implement a function sanitizeHTML(input) that takes a string of untrusted input and returns a sanitized version of the string where the following characters are replaced with their HTML entity equivalents:
& becomes &< becomes <> becomes >" becomes "' becomes '/ becomes /Input: <script>alert('xss')</script>
Output: <script>alert('xss')</script>
Input: Hello & Goodbye
Output: Hello & Goodbye
1 <= input.length <= 10^4You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.
Example 1:
Input: nums = [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2), because they are adjacent (circular).
Example 2:
Input: nums = [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount = 1 + 3 = 4.
Example 3:
Input: nums = [1,2,3]
Output: 3
Explanation: Rob house 2 (money = 3) or house 3 (money = 3).
1 <= nums.length <= 1000 <= nums[i] <= 1000You are given an array of k arrays of integers, where each array is sorted in ascending order.
Merge all the arrays into one sorted array and return it.
Example 1:
Input: arrays = [[1,4,5],[1,3,4],[2,6]]
Output: [1,1,2,3,4,4,5,6]
Explanation: The arrays are:
[
[1,4,5],
[1,3,4],
[2,6]
]
merging them into one sorted list:
[1,1,2,3,4,4,5,6]
Example 2:
Input: arrays = []
Output: []
Example 3:
Input: arrays = [[]]
Output: []
k == arrays.length0 <= k <= 10^40 <= arrays[i].length <= 500-10^4 <= arrays[i][j] <= 10^4arrays[i] is sorted in ascending order.arrays[i].length will not exceed 10^4.Implement the RandomizedSet class:
RandomizedSet() Initializes the RandomizedSet object.bool insert(int val) Inserts an item val into the set if not present. Returns true if the item was not present, false otherwise.bool remove(int val) Removes an item val from the set if present. Returns true if the item was present, false otherwise.int getRandom() Returns a random element from the current set of elements (it's guaranteed that at least one element exists when this method is called). Each element must have the same probability of being returned.You must implement the functions of the class such that each function works in average O(1) time complexity.
Input: ["RandomizedSet", "insert", "remove", "insert", "getRandom", "remove", "insert", "getRandom"]
[[], [1], [2], [2], [], [1], [2], []]
Output: [null, true, false, true, 2, true, false, 2]
Explanation:
RandomizedSet randomizedSet = new RandomizedSet();
randomizedSet.insert(1); // Inserts 1 to the set. Returns true as 1 was inserted successfully.
randomizedSet.remove(2); // Returns false as 2 does not exist in the set.
randomizedSet.insert(2); // Inserts 2 to the set, returns true. Set now contains [1,2].
randomizedSet.getRandom(); // getRandom() should return either 1 or 2 randomly.
randomizedSet.remove(1); // Removes 1 from the set, returns true. Set now contains [2].
randomizedSet.insert(2); // 2 was already in the set, so return false.
randomizedSet.getRandom(); // Since 2 is the only number in the set, getRandom() will always return 2.
-2^31 <= val <= 2^31 - 12 * 10^5 calls will be made to insert, remove, and .Write a SQL query to report the second highest salary from the Employee table. If there is no second highest salary, the query should report null.
The query result format is in the following example.
Table: Employee
| Column Name | Type |
| :--- | :--- |
| id | int |
| salary | int |
id is the primary key column for this table.
Each row of this table contains information about the salary of an employee.
Input:
Employee table:
| id | salary |
| :--- | :--- |
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
Output: | SecondHighestSalary | | :--- | | 200 |
Input:
Employee table:
| id | salary |
| :--- | :--- |
| 1 | 100 |
Output: | SecondHighestSalary | | :--- | | null |
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
Input: s = "anagram", t = "nagaram"
Output: true
Example 2:
Input: s = "rat", t = "car"
Output: false
1 <= s.length, t.length <= 5 * 10^4s and t consist of lowercase English letters.What if the inputs contain Unicode characters? How would you adapt your solution?
Implement a virtual DOM diffing algorithm that compares two virtual DOM trees and produces a minimal set of patches (operations) to transform the old tree into the new tree.
This is the core algorithm behind frameworks like React — it determines what changed between renders and produces efficient updates.
interface VNode {
type: string;
props: Record<string, any>;
children: Array<VNode | string>;
}
type Patch =
| { type: 'CREATE'; node: VNode | string }
| { type: 'REMOVE' }
| { type: 'REPLACE'; node: VNode | string }
| { type: 'UPDATE'; props: PropPatch[]; children: Patch[] }
;
interface PropPatch {
key: string;
value: any; // undefined means remove the prop
}
function diff(oldNode: VNode | string | null, newNode: VNode | string | null): Patch | null
diff(
{ type: 'p', props: {}, children: ['Hello'] },
{ type: 'p', props: {}, children: ['World'] }
);
// UPDATE with child REPLACE 'Hello' -> 'World'
diff(
{ type: 'div', props: {}, children: [] },
{ type: 'span', props: {}, children: [] }
);
// REPLACE entire node
diff(
{ type: 'div', props: { class: 'old' }, children: [] },
{ type: 'div', props: { class: 'new' }, children: [] }
);
// UPDATE with props: [{ key: 'class', value: 'new' }]
The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other.
Given an integer n, return all distinct solutions to the n-queens puzzle. You may return the answer in any order.
Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space, respectively.
Input: n = 4
Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above.
Input: n = 1
Output: [["Q"]]
1 <= n <= 9Design and implement a rate limiting system that controls the rate of API requests from clients. The system should support multiple rate limiting algorithms, per-client and per-endpoint configuration, and work correctly in a distributed multi-server environment.
interface RateLimitConfig {
maxRequests: number; // Maximum requests allowed
windowMs: number; // Time window in milliseconds
algorithm: 'token_bucket' | 'fixed_window' | 'sliding_window';
}
interface RateLimitResult {
allowed: boolean;
remaining: number; // Requests remaining in current window
resetAt: number; // Timestamp when quota resets
retryAfter?: number; // Seconds until next allowed request
}
const limiter = new RateLimiter({ algorithm: 'token_bucket', maxRequests: 10, windowMs: 1000 });
limiter.isAllowed('client-1'); // { allowed: true, remaining: 9 }
// ... 9 more requests ...
limiter.isAllowed('client-1'); // { allowed: false, remaining: 0, retryAfter: 0.1 }
const limiter = new RateLimiter({ algorithm: 'sliding_window', maxRequests: 100, windowMs: 60000 });
// 50 requests in the first 30 seconds
// 60 requests in the next 30 seconds
// At the boundary, sliding window correctly counts overlapping requests
You are given the root of a binary tree containing digits from 0 to 9 only.
Each root-to-leaf path in the tree represents a number.
1 -> 2 -> 3 represents the number 123.Return the total sum of all root-to-leaf numbers. Test cases are generated so that the answer will fit in a 32-bit integer.
A leaf node is a node with no children.
Example 1:
Input: root = [1,2,3]
Output: 25
Explanation:
The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.
Therefore, sum = 12 + 13 = 25.
Example 2:
Input: root = [4,9,0,5,1]
Output: 1026
Explanation:
The root-to-leaf path 4->9->5 represents the number 495.
The root-to-leaf path 4->9->1 represents the number 491.
The root-to-leaf path 4->0 represents the number 40.
Therefore, sum = 495 + 491 + 40 = 1026.
[1, 1000].0 <= Node.val <= 910.getRandomgetRandom is called.limiter.configure([
{ endpoint: '/api/search', maxRequests: 30, windowMs: 60000 },
{ endpoint: '/api/upload', maxRequests: 5, windowMs: 60000 },
{ endpoint: '*', maxRequests: 100, windowMs: 60000 },
]);