319 challenges in the catalogue. Browse freely; sign in to open the editor and submit.
Build a function that generates a responsive CSS grid system. Given a configuration with column count, gap size, and breakpoints, generate the CSS rules that create a responsive grid layout — similar to Bootstrap's grid but using CSS Grid.
interface GridConfig {
columns: number;
gap: string;
breakpoints: Record<string, number>;
}
function createGrid(config: GridConfig): string
config.columns — Total number of columns (e.g., 12).config.gap — Gap between grid items (e.g., '16px').config.breakpoints — Named breakpoints with min-width values (e.g., { sm: 640, md: 768, lg: 1024 }).A CSS string that defines:
.grid container class with CSS Grid layout.col-{n} classes for spanning n columns (1 to max).col-{breakpoint}-{n} classes within media queriesconst css = createGrid({
columns: 12,
gap: '16px',
breakpoints: { sm: 640, md: 768 }
});
Generated CSS should include:
.grid {
display: grid;
grid-template-columns: repeat(12, 1fr);
gap: 16px;
}
.col-1 { grid-column: span 1; }
.col-2 { grid-column: span 2; }
/* ... up to .col-12 */
@media (min-width: 640px) {
.col-sm-1 { grid-column: span 1; }
/* ... */
}
@media (min-width: 768px) {
.col-md-1 { grid-column: span 1; }
/* ... */
}
1 <= columns <= 24Build a distributed tracing system that tracks requests across microservices. Implement span creation, context propagation, and trace analysis following OpenTelemetry conventions.
Implement a tracing library that can:
traceparent header format: {version}-{traceId}-{spanId}-{flags}Example 1:
Input: Creating a span
const tracer = createTracer("api-gateway");
const span = tracer.startSpan("GET /users");
span.setAttribute("http.method", "GET");
span.end();
Output: Span object with traceId, spanId, name, startTime, endTime, attributes
Given an integer n, return the number of trailing zeroes in n!.
Note that n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1.
Input: n = 3
Output: 0
Explanation: 3! = 6, no trailing zero.
Input: n = 5
Output: 1
Explanation: 5! = 120, one trailing zero.
Input: n = 0
Output: 0
0 <= n <= 10^4Follow up: Could you write an algorithm that runs in logarithmic time complexity?
CREATE TABLE Students (
student_id INT PRIMARY KEY,
preference INT -- 0 for circular, 1 for square
);
CREATE TABLE Sandwiches (
sandwich_id INT PRIMARY KEY,
type INT, -- 0 for circular, 1 for square
position INT -- position in the stack (1 = top)
);
The school cafeteria offers circular and square sandwiches (represented by 0 and 1 respectively). Students stand in a queue and each prefers either circular or square. The sandwich on top of the stack is served. If the student at the front of the queue prefers it, they take it and leave. Otherwise, they go to the end of the queue.
This continues until none of the queue students want the top sandwich — the remaining students cannot eat.
Given the Students table with their preferences and the Sandwiches table with sandwich types and their positions in the stack, write a query to count the number of students who are unable to eat.
Input:
Students table: | student_id | preference | |------------|------------| | 1 | 1 | | 2 | 1 | | 3 | 0 | | 4 | 0 |
Sandwiches table: | sandwich_id | type | position | |-------------|------|----------| | 1 | 0 | 1 | | 2 | 1 | 2 | | 3 | 0 | 3 | | 4 | 1 | 4 |
Output:
| count | |-------| | 0 |
Explanation: All students can eat. The circular sandwiches (type 0) match students with preference 0, and square sandwiches match students with preference 1.
Input:
Students table: | student_id | preference | |------------|------------| | 1 | 1 | | 2 | 1 | | 3 | 1 |
Sandwiches table: | sandwich_id | type | position | |-------------|------|----------| | 1 | 0 | 1 | | 2 | 1 | 2 | | 3 | 1 | 3 |
Output:
| count | |-------| | 1 |
Explanation: The top sandwich is type 0, but no remaining student wants it, so 1 student who wants type 0 cannot eat. Wait, all students want type 1 — so 1 student is left because there are 3 students wanting type 1 but only 2 type-1 sandwiches.
In a distributed system, generating unique identifiers (IDs) at scale is a common requirement. These IDs are often used as primary keys in databases.
Traditional auto-incrementing IDs in a single database don't scale globally and create a single point of failure. Your goal is to design a system that can generate 64-bit unique IDs across multiple servers with the following properties:
Implement a generateID() function that follows the Snowflake pattern.
1 bit: Reserved (usually 0).41 bits: Timestamp (milliseconds since a custom epoch).10 bits: Worker/Machine ID (allows for 1,024 nodes).12 bits: Sequence number (resets every millisecond).You are assigned to put some amount of boxes onto one truck. You are given a 2D array boxTypes, where boxTypes[i] = [numberOfBoxes_i, numberOfUnitsPerBox_i]:
numberOfBoxes_i is the number of boxes of type i.numberOfUnitsPerBox_i is the number of units in each box of type i.You are also given an integer truckSize, which is the maximum number of boxes that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed truckSize.
Return the maximum total number of units that can be put on the truck.
Input: boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
Output: 8
Explanation:
Input: boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
Output: 91
1 <= boxTypes.length <= 10001 <= numberOfBoxes_i, numberOfUnitsPerBox_i <= 10001 <= truckSize <= 10^6A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root.
The path sum of a path is the sum of the node's values in the path.
Given the root of a binary tree, return the maximum path sum of any non-empty path.
Example 1:
Input: root = [1,2,3]
Output: 6
Explanation: The optimal path is 2 -> 1 -> 3 with a path sum of 2 + 1 + 3 = 6.
Example 2:
Input: root = [-3]
Output: -3
Example 3:
Input: root = [-10,9,20,null,null,15,7]
Output: 42
Explanation: The optimal path is 15 -> 20 -> 7 with a path sum of 15 + 20 + 7 = 42.
[1, 3 * 10^4].-1000 <= Node.val <= 1000You are given an integer array prices where prices[i] is the price of a given stock on the ith day, and an integer k.
Find the maximum profit you can achieve. You may complete at most k transactions.
Note: You may not engage in multiple transactions simultaneously.
Example 1:
Input: k = 2, prices = [2,4,1]
Output: 2
Explanation: Buy on day 1 (price=2), sell on day 2 (price=4), profit=2.
Example 2:
Input: k = 2, prices = [3,2,6,5,0,3]
Output: 7
Explanation: Buy day 2 (price=2), sell day 3 (price=6), profit=4.
Buy day 5 (price=0), sell day 6 (price=3), profit=3. Total=7.
1 <= k <= 1001 <= prices.length <= 10000 <= prices[i] <= 1000CREATE 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)
);
A company's executives are interested in seeing who earns the most in each department. A high earner in a department is an employee who has a salary in the top three unique salaries for that department.
Write a SQL query to find the employees who are high earners in each of the departments.
Return the result table with columns Department, Employee, and Salary in any order.
Input:
Employee table: | id | name | salary | departmentId | |----|-------|--------|--------------| | 1 | Joe | 85000 | 1 | | 2 | Henry | 80000 | 2 | | 3 | Sam | 60000 | 2 | | 4 | Max | 90000 | 1 | | 5 | Janet | 69000 | 1 | | 6 | Randy | 85000 | 1 | | 7 | Will | 70000 | 1 |
Department table: | id | name | |----|-------| | 1 | IT | | 2 | Sales |
Output:
| Department | Employee | Salary | |------------|----------|--------| | IT | Max | 90000 | | IT | Joe | 85000 | | IT | Randy | 85000 | | IT | Will | 70000 | | Sales | Henry | 80000 | | Sales | Sam | 60000 |
Explanation:
CREATE TABLE Views (
article_id INT,
author_id INT,
viewer_id INT,
view_date DATE
);
There is no primary key for this table; it may have duplicate rows.
Each row of this table indicates that some viewer viewed an article (written by some author) on some date.
Note that equal author_id and viewer_id indicate the same person.
Write a SQL query to find all the authors that viewed at least one of their own articles.
Return the result table sorted by id in ascending order.
Input:
Views table: | article_id | author_id | viewer_id | view_date | |------------|-----------|-----------|------------| | 1 | 3 | 5 | 2019-08-01 | | 1 | 3 | 6 | 2019-08-02 | | 2 | 7 | 7 | 2019-08-01 | | 2 | 7 | 6 | 2019-08-02 | | 4 | 7 | 1 | 2019-07-22 | | 3 | 4 | 4 | 2019-07-21 | | 3 | 4 | 4 | 2019-07-21 |
Output:
| id | |----| | 4 | | 7 |
Explanation: Author 4 viewed article 3 on 2019-07-21, and author 7 viewed article 2 on 2019-08-01.
article_id, author_id, and viewer_id are positive integers.You are given the head of a singly linked-list. The list can be represented as:
L_0 → L_1 → … → L_{n - 1} → L_n
Reorder the list to be on the following form:
L_0 → L_n → L_1 → L_{n - 1} → L_2 → L_{n - 2} → …
You may not modify the values in the list's nodes. Only nodes themselves may be changed.
Example 1:
Input: head = [1,2,3,4]
Output: [1,4,2,3]
Example 2:
Input: head = [1,2,3,4,5]
Output: [1,5,2,4,3]
[1, 5 * 10^4].1 <= Node.val <= 1000CREATE TABLE Seat (
id SERIAL PRIMARY KEY,
student VARCHAR(255)
);
id is a continuous auto-increment column starting from 1.
Write a SQL query to swap the seat id of every two consecutive students. If the number of students is odd, the id of the last student is not swapped.
Return the result table ordered by id in ascending order.
Input:
Seat table: | id | student | |----|---------| | 1 | Abbot | | 2 | Doris | | 3 | Emerson | | 4 | Green | | 5 | Jeames |
Output:
| id | student | |----|---------| | 1 | Doris | | 2 | Abbot | | 3 | Green | | 4 | Emerson | | 5 | Jeames |
Explanation: Students in seats 1 and 2 swap, students in seats 3 and 4 swap. Student in seat 5 stays because there is no seat 6 to swap with.
Input:
Seat table: | id | student | |----|---------| | 1 | Alice | | 2 | Bob |
Output:
| id | student | |----|---------| | 1 | Bob | | 2 | Alice |
id is continuous starting from 1.Example 2:
Input: Context propagation
const headers = { traceparent: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01" };
const context = tracer.extractContext(headers);
const childSpan = tracer.startSpan("db-query", context);
Output: Child span with same traceId, parent spanId = 00f067aa0ba902b7
00-{traceId}-{spanId}-{flags}