319 challenges in the catalogue. Browse freely; sign in to open the editor and submit.
Build a GraphQL API server for a social platform with users, posts, and comments. Implement a type schema, resolvers for queries and mutations, handle nested relationships efficiently using DataLoader, and implement proper error handling.
type User {
id: ID!
name: String!
email: String!
posts: [Post!]!
createdAt: String!
}
type Post {
id: ID!
title: String!
content: String!
author: User!
comments: [Comment!]!
createdAt: String!
}
type Comment {
id: ID!
text: String!
author: User!
post: Post!
createdAt: String!
}
type Query {
user(id: ID!): User
users(limit: Int, offset: Int): [User!]!
post(id: ID!): Post
posts(authorId: ID, limit: Int): [Post!]!
}
type Mutation {
createUser(input: CreateUserInput!): User!
createPost(input: CreatePostInput!): Post!
createComment(input: CreateCommentInput!): Comment!
updatePost(id: ID!, input: UpdatePostInput!): Post!
deletePost(id: ID!): Boolean!
}
query {
user(id: "1") {
name
posts {
title
comments {
text
author { name }
}
}
}
}
Response:
{
"data": {
"user": {
"name": "Alice",
"posts": [{
"title": "GraphQL Basics",
"comments": [{
"text": "Great article!",
"author": { "name": "Bob" }
}]
}]
}
}
}
mutation {
createPost(input: { title: "New Post", content: "Content here", authorId: "1" }) {
id
title
author { name }
}
}
query { user(id: "999") { name } }
Response:
{
"data": { "user": null },
"errors": [{ "message": "User not found", "path": ["user"] }]
}
Design a distributed, in-memory key-value cache system similar to Redis or Memcached. The system should provide low-latency data access and be scalable across multiple nodes to handle massive amounts of data and high request rates.
get, set, and delete.Example Scenario:
cache.set("user:101", { name: "John Doe" }, 300)cache.get("user:101"){ name: "John Doe" }cache.get("user:101") returns null.CREATE TABLE Sales (
sale_id INT,
product_id INT REFERENCES Product(product_id),
year INT,
quantity INT,
price INT,
PRIMARY KEY (sale_id, year)
);
CREATE TABLE Product (
product_id INT PRIMARY KEY,
product_name VARCHAR(255)
);
Write a SQL query that reports the product_name, year, and price for each sale_id in the Sales table.
Return the result table in any order.
Input:
Sales table: | sale_id | product_id | year | quantity | price | |---------|------------|------|----------|-------| | 1 | 100 | 2008 | 10 | 5000 | | 2 | 100 | 2009 | 12 | 5000 | | 7 | 200 | 2011 | 15 | 9000 |
Product table: | product_id | product_name | |------------|--------------| | 100 | Nokia | | 200 | Apple | | 300 | Samsung |
Output:
| product_name | year | price | |--------------|------|-------| | Nokia | 2008 | 5000 | | Nokia | 2009 | 5000 | | Apple | 2011 | 9000 |
Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the ith day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead.
Example 1:
Input: temperatures = [73,74,75,71,69,72,76,73]
Output: [1,1,4,2,1,1,0,0]
Example 2:
Input: temperatures = [30,40,50,60]
Output: [1,1,1,0]
Example 3:
Input: temperatures = [30,60,90]
Output: [1,1,0]
1 <= temperatures.length <= 10^530 <= temperatures[i] <= 100Design a time-based key-value data structure that can store multiple values for the same key at different time stamps and retrieve the key's value at a certain timestamp.
Implement the TimeMap class:
TimeMap() Initializes the object of the data structure.void set(String key, String value, int timestamp) Stores the key key with the value value at the given time timestamp.String get(String key, int timestamp) Returns a value such that set was called previously, with timestamp_prev <= timestamp. If there are multiple such values, it returns the value associated with the largest timestamp_prev. If there are no such values, it returns "".Input: ["TimeMap", "set", "get", "get", "set", "get", "get"]
[[], ["foo", "bar", 1], ["foo", 1], ["foo", 3], ["foo", "bar2", 4], ["foo", 4], ["foo", 5]]
Output: [null, null, "bar", "bar", null, "bar2", "bar2"]
Explanation:
TimeMap timeMap = new TimeMap();
timeMap.set("foo", "bar", 1); // store the key "foo" and value "bar" along with timestamp = 1.
timeMap.get("foo", 1); // return "bar"
timeMap.get("foo", 3); // return "bar", since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 is "bar".
timeMap.set("foo", "bar2", 4); // store the key "foo" and value "bar2" along with timestamp = 4.
timeMap.get("foo", 4); // return "bar2"
timeMap.get("foo", 5); // return "bar2"
1 <= key.length, value.length <= 100key and value consist of lowercase English letters and digits.1 <= timestamp <= 10^7timestamp of set are strictly increasing.2 * 10^5 calls will be made to and .Given the head of a linked list, remove the $n^{th}$ node from the end of the list and return its head.
Example 1:
Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]
Example 2:
Input: head = [1], n = 1
Output: []
Example 3:
Input: head = [1,2], n = 1
Output: [1]
sz.1 <= sz <= 300 <= Node.val <= 1001 <= n <= szCould you do this in one pass?
Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length.
Return the indices of the two numbers, index1 and index2, as an integer array [index1, index2] of length 2.
The tests are generated such that there is exactly one solution. You may not use the same element twice.
Your solution must use only constant extra space.
Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2].
Input: numbers = [2,3,4], target = 6
Output: [1,3]
Explanation: The sum of 2 and 4 is 6. Therefore, index1 = 1, index2 = 3. We return [1, 3].
Input: numbers = [-1,0], target = -1
Output: [1,2]
2 <= numbers.length <= 3 * 10^4-1000 <= numbers[i] <= 1000numbers is sorted in non-decreasing order.-1000 <= target <= 1000Implement a simplified version of React's useState hook. Your implementation should maintain state across "re-renders" (repeated function calls) and support both direct value updates and functional updates.
You will need to implement a mini hooks system that tracks state across calls.
function useState<T>(initialValue: T): [T, (newValue: T | ((prev: T) => T)) => void]
initialValue — The initial state value (used only on the first render).A tuple [state, setState] where:
state — The current state value.setState — A function to update the state. Accepts either a new value or an updater function (prevState) => newState.You also need to implement:
createHooksSystem() — Returns { useState, render } where render(component) simulates rendering.setState should trigger a re-render.const { useState, render } = createHooksSystem();
function Counter() {
const [count, setCount] = useState(0);
return { count, increment: () => setCount(count + 1) };
}
let result = render(Counter);
console.log(result.count); // 0
result.increment();
result = render(Counter);
console.log(result.count); // 1
function Counter() {
const [count, setCount] = useState(0);
return {
count,
incrementBy: (n: number) => setCount(prev => prev + n)
};
}
function Form() {
const [name, setName] = useState('');
const [age, setAge] = useState(0);
return { name, age, setName, setAge };
}
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
Example 1:
Input: grid = [[1,3,1],[1,5,1],[4,2,1]]
Output: 7
Explanation: The path 1 -> 3 -> 1 -> 1 -> 1 minimizes the sum.
Example 2:
Input: grid = [[1,2,3],[4,5,6]]
Output: 12
m == grid.lengthn == grid[i].length1 <= m, n <= 2000 <= grid[i][j] <= 200CREATE TABLE Person (
id INT PRIMARY KEY,
email VARCHAR(255)
);
id is the primary key column for this table. Each row contains an email. Emails will not contain uppercase letters.
Write a SQL query to delete all duplicate email entries in the Person table, keeping only one unique email with the smallest id.
Note that you are asked to write a DELETE statement, not a SELECT statement.
Input:
Person table: | id | email | |----|------------------| | 1 | john@example.com | | 2 | bob@example.com | | 3 | john@example.com |
Output:
| id | email | |----|------------------| | 1 | john@example.com | | 2 | bob@example.com |
Explanation: john@example.com is duplicated. We keep the row with the smallest id (id=1) and delete id=3.
Input:
Person table: | id | email | |----|--------------| | 1 | a@b.com | | 2 | c@d.com | | 3 | a@b.com | | 4 | c@d.com | | 5 | a@b.com |
Output:
| id | email | |----|--------------| | 1 | a@b.com | | 2 | c@d.com |
Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list.
Example 1:
Input: head = [1,2,3,4,5], left = 2, right = 4
Output: [1,4,3,2,5]
Example 2:
Input: head = [5], left = 1, right = 1
Output: [5]
n.1 <= n <= 500-500 <= Node.val <= 5001 <= left <= right <= nFollow up: Could you do it in one pass?
Given the root of a binary tree, return its maximum depth.
A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Example 1:
Input: root = [3,9,20,null,null,15,7]
Output: 3
Example 2:
Input: root = [1,null,2]
Output: 2
[0, 10^4].-100 <= Node.val <= 100setget