319 challenges in the catalogue. Browse freely; sign in to open the editor and submit.
Implement a curry function that transforms a function so it can be called with its arguments one at a time (or in groups). A curried function keeps returning new functions until all expected arguments have been provided, at which point it returns the result.
function curry(fn: (...args: any[]) => any): (...args: any[]) => any
fn — A function with a fixed number of parameters (determined by fn.length).A curried version of fn that accumulates arguments across calls and invokes fn once all arguments have been provided.
function add(a: number, b: number, c: number): number {
return a + b + c;
}
const curriedAdd = curry(add);
curriedAdd(1)(2)(3); // 6
curriedAdd(1, 2)(3); // 6
curriedAdd(1, 2, 3); // 6
function multiply(a: number, b: number): number {
return a * b;
}
const double = curry(multiply)(2);
double(5); // 10
double(10); // 20
fn.length > 0fn.length to determine when to invoke the original functionDesign a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.
Implement KthLargest class:
constructor(k: number, nums: number[]) Initializes the object with the integer k and the stream of integers nums.add(val: number): number Appends the integer val to the stream and returns the element representing the kth largest element in the stream.Example 1:
Input:
["KthLargest", "add", "add", "add", "add", "add"]
[[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]]
Output:
[null, 4, 5, 5, 8, 8]
Explanation:
KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]);
kthLargest.add(3); // return 4
kthLargest.add(5); // return 5
kthLargest.add(10); // return 5
kthLargest.add(9); // return 8
kthLargest.add(4); // return 8
1 <= k <= 10^40 <= nums.length <= 10^4-10^4 <= nums[i] <= 10^4-10^4 <= val <= 10^410^4 calls will be made to add.k elements in the array when you search for the kth element.Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.
Input: s = "aab"
Output: [["a","a","b"],["aa","b"]]
Input: s = "a"
Output: [["a"]]
1 <= s.length <= 16s contains only lowercase English letters.Given the roots of two binary trees root and subRoot, return true if there is a subtree of root with the same structure and node values of subRoot and false otherwise.
A subtree of a binary tree tree is a tree that consists of a node in tree and all of this node's descendants. The tree tree could also be considered as a subtree of itself.
Example 1:
Input: root = [3,4,5,1,2], subRoot = [4,1,2]
Output: true
Example 2:
Input: root = [3,4,5,1,2,null,null,null,null,0], subRoot = [4,1,2]
Output: false
Explanation: Although the tree has [4,1,2], the 2 node has a left child 0, so it is not a perfect match.
root is in the range [1, 2000].subRoot is in the range [1, 1000].-10^4 <= root.val <= 10^4-10^4 <= subRoot.val <= 10^4SQL injection is one of the most common and dangerous web vulnerabilities (OWASP Top 10 #3). Your task is to identify vulnerable SQL queries and rewrite them using secure patterns.
Given vulnerable SQL query patterns that concatenate user input directly into SQL strings, transform them into secure parameterized queries. Additionally, implement input validation functions that reject malicious input.
? placeholders with separate parameter arrays' OR 1=1, ; DROP TABLE, UNION SELECT, etc.)Example 1 -- Basic Injection:
Vulnerable:
"SELECT * FROM users WHERE username = '" + input + "'"
Attack: input = "' OR '1'='1"
Result: SELECT * FROM users WHERE username = '' OR '1'='1' (returns all users!)
Secure:
query: "SELECT * FROM users WHERE username = ?"
params: [input]
Example 2 -- Login Bypass:
Vulnerable:
"SELECT * FROM users WHERE email = '" + email + "' AND password = '" + pwd + "'"
Attack: email = "admin'--", pwd = "anything"
Result: SELECT * FROM users WHERE email = 'admin'--' AND password = 'anything'
Secure:
query: "SELECT * FROM users WHERE email = ? AND password = ?"
params: [email, pwd]
Example 3 -- UNION Attack:
Vulnerable:
"SELECT name, price FROM products WHERE id = " + id
Attack: id = "1 UNION SELECT username, password FROM users"
Secure:
query: "SELECT name, price FROM products WHERE id = ?"
params: [parseInt(id)]
Given an array of meeting time intervals intervals where intervals[i] = [start_i, end_i], return the minimum number of conference rooms required.
Example 1:
Input: intervals = [[0,30],[5,10],[15,20]]
Output: 2
Explanation: Meeting [0,30] overlaps with [5,10] and [15,20], but [5,10] and [15,20] don't overlap with each other. We need 2 rooms.
Example 2:
Input: intervals = [[7,10],[2,4]]
Output: 1
Explanation: The meetings don't overlap, so 1 room is sufficient.
Example 3:
Input: intervals = [[0,5],[5,10],[10,15]]
Output: 1
Explanation: Each meeting ends before the next starts (a meeting ending at 5 doesn't conflict with one starting at 5).
1 <= intervals.length <= 10^40 <= start_i < end_i <= 10^6There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.
Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner.
Example 1:
Input: m = 3, n = 7
Output: 28
Example 2:
Input: m = 3, n = 2
Output: 3
Explanation: From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -> Down -> Down
2. Down -> Down -> Right
3. Down -> Right -> Down
1 <= m, n <= 100CREATE TABLE Person (
id INT PRIMARY KEY,
email VARCHAR(255)
);
id is the primary key column. Each row contains an email. Emails will not contain uppercase letters.
Write a SQL query to report all duplicate emails. A duplicate email is one that appears more than once in the table.
Return the result table with the column name Email in any order.
Input:
Person table: | id | email | |----|---------| | 1 | a@b.com | | 2 | c@d.com | | 3 | a@b.com |
Output:
| Email | |---------| | a@b.com |
Explanation: a@b.com appears twice, so it is a duplicate.
Input:
Person table: | id | email | |----|--------------| | 1 | joe@mail.com | | 2 | bob@mail.com | | 3 | joe@mail.com | | 4 | joe@mail.com | | 5 | bob@mail.com |
Output:
| Email | |--------------| | joe@mail.com | | bob@mail.com |
CREATE TABLE Insurance (
pid INT PRIMARY KEY,
tiv_2015 FLOAT,
tiv_2016 FLOAT,
lat FLOAT,
lon FLOAT
);
pid is the primary key.tiv_2015 is the total investment value in 2015.tiv_2016 is the total investment value in 2016.lat and lon are the latitude and longitude of the policyholder's city. Pairs of (lat, lon) are guaranteed to be unique.Write a SQL query to report the sum of all total investment values in 2016 (tiv_2016), for all policyholders who:
tiv_2015 value as one or more other policyholders, ANDRound the result to 2 decimal places.
Input:
Insurance table: | pid | tiv_2015 | tiv_2016 | lat | lon | |-----|----------|----------|-----|-----| | 1 | 10 | 5 | 10 | 10 | | 2 | 20 | 20 | 20 | 20 | | 3 | 10 | 30 | 20 | 20 | | 4 | 10 | 40 | 40 | 40 |
Output:
| tiv_2016 | |----------| | 45.00 |
Explanation:
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
Implement the MinStack class:
MinStack() initializes the stack object.push(val) pushes the element val onto the stack.pop() removes the element on the top of the stack.top() gets the top element of the stack.getMin() retrieves the minimum element in the stack.You must implement a solution with O(1) time complexity for each function.
Example 1:
Input:
["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[]]
Output: [null,null,null,null,-3,null,0,-2]
Explanation:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); // return -3
minStack.pop();
minStack.top(); // return 0
minStack.getMin(); // return -2
-2^31 <= val <= 2^31 - 1pop, top and getMin operations will always be called on non-empty stacks.3 * 10^4 calls will be made to push, pop, top, and getMin.Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*'.
'?' Matches any single character.'*' Matches any sequence of characters (including the empty sequence).The matching should cover the entire input string (not partial).
Input: s = "aa", p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".
Input: s = "aa", p = "*"
Output: true
Explanation: '*' matches any sequence.
Input: s = "cb", p = "?a"
Output: false
Explanation: '?' matches 'c', but the second letter 'a' does not match 'b'.
0 <= s.length, p.length <= 2000s contains only lowercase English letters.p contains lowercase English letters, '?' or '*'.You have some number of sticks with positive integer lengths. These lengths are given as an array sticks, where sticks[i] is the length of the $i^{th}$ stick.
You can connect any two sticks of lengths x and y into one stick by paying a cost of x + y. You must connect all the sticks until there is only one stick remaining.
Return the minimum cost of connecting all the given sticks into one stick in this way.
Input: sticks = [2,4,3]
Output: 14
Explanation:
Input: sticks = [1,8,3,5]
Output: 30
1 <= sticks.length <= 10^41 <= sticks[i] <= 10^4