319 challenges in the catalogue. Browse freely; sign in to open the editor and submit.
Reverse bits of a given 32-bit unsigned integer.
Input: n = 43261596 (binary 00000010100101000001111010011100)
Output: 964176192 (binary 00111001011110000010100101000000)
Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return its reverse binary string 00111001011110000010100101000000 which represents the unsigned integer 964176192.
32.CREATE TABLE Transactions (
id INT PRIMARY KEY,
country VARCHAR(4),
state VARCHAR(10), -- 'approved' or 'declined'
amount INT,
trans_date DATE
);
Write a SQL query to find for each month and country, the number of transactions and their total amount, the number of approved transactions and their total amount.
Return the result table with columns: month, country, trans_count, approved_count, trans_total_amount, approved_total_amount.
The month column should be in YYYY-MM format.
Input:
Transactions table: | id | country | state | amount | trans_date | |-----|---------|----------|--------|------------| | 121 | US | approved | 1000 | 2018-12-18 | | 122 | US | declined | 2000 | 2018-12-19 | | 123 | US | approved | 2000 | 2019-01-01 | | 124 | DE | approved | 2000 | 2019-01-07 |
Output:
| month | country | trans_count | approved_count | trans_total_amount | approved_total_amount | |---------|---------|-------------|----------------|--------------------|-----------------------| | 2018-12 | US | 2 | 1 | 3000 | 1000 | | 2019-01 | US | 1 | 1 | 2000 | 2000 | | 2019-01 | DE | 1 | 1 | 2000 | 2000 |
state is either 'approved' or 'declined'.Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).
Example 1:
Input: root = [1,2,2,3,4,4,3]
Output: true
Example 2:
Input: root = [1,2,2,null,3,null,3]
Output: false
[1, 1000].-100 <= Node.val <= 100Could you solve it both recursively and iteratively?
Given an array of integers preorder, which represents the preorder traversal of a BST (Binary Search Tree), construct the tree and return its root.
It is guaranteed that for the given test cases, there is always possible to find a binary search tree with the given properties.
Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has a value < node.val, and any descendant of node.right has a value > node.val. Also, a preorder traversal of a binary tree displays the value of the node first, then traverses node.left, then traverses node.right.
Input: preorder = [8,5,1,7,10,12]
Output: [8,5,10,1,7,null,12]
Input: preorder = [1,3]
Output: [1,null,3]
1 <= preorder.length <= 1001 <= preorder[i] <= 10^8preorder are unique.Given an input string s, reverse the order of the words.
A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space.
Return a string of the words in reverse order concatenated by a single space.
Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.
Example 1:
Input: s = "the sky is blue"
Output: "blue is sky the"
Example 2:
Input: s = " hello world "
Output: "world hello"
Explanation: Your reversed string should not contain leading or trailing spaces.
Example 3:
Input: s = "a good example"
Output: "example good a"
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.
1 <= s.length <= 10^4s contains English letters (upper-case and lower-case), digits, and spaces ' '.s.If the string data type is mutable in your language, can you solve it in-place with $O(1)$ extra space?
Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that:
0 <= a, b, c, d < na, b, c, and d are distinct.nums[a] + nums[b] + nums[c] + nums[d] == targetYou may return the answer in any order.
Input: nums = [1,0,-1,0,-2,2], target = 0
Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
Input: nums = [2,2,2,2,2], target = 8
Output: [[2,2,2,2]]
1 <= nums.length <= 200-10^9 <= nums[i] <= 10^9-10^9 <= target <= 10^9Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.
Input: nums = [3,0,1]
Output: 2
Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums.
Input: nums = [0,1]
Output: 2
Explanation: n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums.
Input: nums = [9,6,4,2,3,5,7,0,1]
Output: 8
Explanation: n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums.
n == nums.length1 <= n <= 10^40 <= nums[i] <= nnums are unique.Follow up: Could you implement a solution using only $O(1)$ extra space complexity and $O(n)$ runtime complexity?
You 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.
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
Input: nums = [1], k = 1
Output: [1]
1 <= nums.length <= 10^5-10^4 <= nums[i] <= 10^41 <= k <= nums.lengthImplement an infinite scroll system that loads data in pages as the user scrolls. Build a controller that manages pagination state, loading status, and data accumulation.
interface InfiniteScrollConfig {
fetchPage: (page: number, pageSize: number) => Promise<any[]>;
pageSize: number;
}
interface InfiniteScrollController {
loadMore(): Promise<void>;
getItems(): any[];
hasMore(): boolean;
isLoading(): boolean;
reset(): void;
}
function createInfiniteScroll(config: InfiniteScrollConfig): InfiniteScrollController
loadMore() — Fetches the next page and appends results. No-op if already loading or no more data.getItems() — Returns all loaded items so far.hasMore() — Returns true if more data might be available.isLoading() — Returns true if a fetch is in progress.reset() — Clears all loaded data and resets to page 1.const scroll = createInfiniteScroll({
fetchPage: async (page, size) => mockData.slice((page-1)*size, page*size),
pageSize: 10
});
await scroll.loadMore(); // Loads items 1-10
console.log(scroll.getItems().length); // 10
await scroll.loadMore(); // Loads items 11-20
console.log(scroll.getItems().length); // 20
// If fetchPage returns fewer items than pageSize, hasMore becomes false
await scroll.loadMore(); // Returns 5 items (pageSize is 10)
scroll.hasMore(); // false
scroll.loadMore(); // Started loading
scroll.loadMore(); // No-op (already loading)
scroll.isLoading(); // true
pageSize >= 1fetchPage returns a Promise resolving to an arrayGiven a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Explanation: The LCA of nodes 5 and 1 is 3.
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
Output: 5
Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
Input: root = [1,2], p = 1, q = 2
Output: 1
[2, 10^5].-10^9 <= Node.val <= 10^9Node.val are unique.p != qp and q will exist in the tree.You are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation.
Evaluate the expression. Return an integer that represents the value of the expression.
Note:
+, -, *, and /.Example 1:
Input: tokens = ["2","1","+","3","*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9
Example 2:
Input: tokens = ["4","13","5","/","+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6
Example 3:
Input: tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
Output: 22
Explanation: ((10 * (6 / ((9 + 3) * -11))) + 17 + 5) = 22
1 <= tokens.length <= 10^4tokens[i] is either an operator (+, -, *, /) or an integer in the range [-200, 200].Build a Feature Engineering Pipeline that applies a sequence of transformations to raw data and outputs a numerical feature matrix. The pipeline should support multiple transformation types and handle both numerical and categorical data.
function featureEngineeringPipeline(
data: any[][],
columns: string[],
operations: string[]
): { features: number[][]; column_names: string[] }
data: 2D array of raw data (rows x columns), can contain numbers and stringscolumns: Column names corresponding to each column in dataoperations: List of transformations to apply in orderInput:
data = [[1,100,"cat"],[2,200,"dog"],[3,300,"cat"]]
columns = ["age","salary","pet"]
operations = ["normalize:age","normalize:salary","one_hot:pet"]
Output:
features = [[0,0,1,0],[0.5,0.5,0,1],[1,1,1,0]]
column_names = ["age_norm","salary_norm","pet_cat","pet_dog"]