319 challenges in the catalogue. Browse freely; sign in to open the editor and submit.
Implement a deep link parser for a mobile application. Given a URL and a set of route configurations, parse the URL to determine which screen to navigate to and extract path parameters and query parameters.
myapp://path/to/screen):param)interface Route {
pattern: string; // e.g., "products/:id"
screen: string; // e.g., "ProductDetail"
}
function parseDeepLink(
url: string,
routes: Route[]
): { screen: string; params: Record<string, string>; query: Record<string, string> } | null
Input:
url = "myapp://products/123?ref=home"
routes = [{ pattern: "products/:id", screen: "ProductDetail" }]
Output:
{ screen: "ProductDetail", params: { id: "123" }, query: { ref: "home" } }
scheme://path?query:paramName for dynamic segmentsYou are visiting a farm that has a single row of fruit trees arranged from left to right. The trees are represented by an integer array fruits where fruits[i] is the type of fruit the $i^{th}$ tree produces.
You want to collect as much fruit as possible. However, the owner has some strict rules that you must follow:
Given the integer array fruits, return the maximum number of fruits you can pick.
Input: fruits = [1,2,1]
Output: 3
Explanation: We can pick from all 3 trees.
Input: fruits = [0,1,2,2]
Output: 3
Explanation: We can pick from trees [1,2,2]. If we had started at tree 0, we would only pick [0,1].
Input: fruits = [1,2,3,2,2]
Output: 4
Explanation: We can pick from trees [2,3,2,2]. If we had started at the first tree, we would only pick [1,2].
1 <= fruits.length <= 10^50 <= fruits[i] < fruits.lengthYou are given a string s. We want to partition the string into as many parts as possible so that each letter appears in at most one part.
Note that the partition is done so that after concatenating all the parts in order, the resultant string should be s.
Return a list of integers representing the size of these parts.
Input: s = "ababcbacadefegdehijhklij"
Output: [9,7,8]
Explanation:
The partition is "ababcbaca", "defegde", "hijhklij".
This is a partition so that each letter appears in at most one part.
A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits s into less parts.
Input: s = "eccbbbbdec"
Output: [10]
1 <= s.length <= 500s consists of lowercase English letters.Implement a throttle function that limits the rate at which a function can fire. The throttled function will only invoke the original function at most once per every interval milliseconds.
Unlike debounce (which delays execution until activity stops), throttle guarantees that the function fires at a regular interval during continuous activity.
function throttle(fn: (...args: any[]) => void, interval: number): (...args: any[]) => void
fn — The function to throttle.interval — The minimum time in milliseconds between invocations.A throttled function. The first call executes immediately. Subsequent calls during the interval are ignored, but the last call in a series will fire after the interval elapses.
let count = 0;
const increment = throttle(() => { count++; }, 100);
increment(); // Executes immediately, count = 1
increment(); // Ignored (within 100ms)
increment(); // Ignored (within 100ms)
// After 100ms: trailing call fires, count = 2
const log = throttle((msg: string) => console.log(msg), 200);
log("a"); // Logs "a" immediately
// 50ms later
log("b"); // Saved as trailing call
// 100ms later
log("c"); // Replaces trailing call
// At 200ms: logs "c" (trailing)
const onScroll = throttle(updatePosition, 16); // ~60fps
window.addEventListener('scroll', onScroll);
// updatePosition fires at most once every 16ms during scrolling
0 <= interval <= 10000fn is a valid callable functionfnThe count-and-say sequence is a sequence of digit strings defined by the recursive formula:
countAndSay(1) = "1"countAndSay(n) is the way you would "say" the digit string from countAndSay(n-1), which is then converted into a different digit string.To determine how you "say" a digit string, split it into the minimal number of substrings such that each substring contains exactly one unique digit. Then for each substring, say the number of digits, then say the digit. Finally, concatenate every said digit.
For example, the saying and conversion for digit string "3322251":
"23" + "32" + "15" + "11""23321511"Given a positive integer n, return the $n^{th}$ term of the count-and-say sequence.
Example 1:
Input: n = 1
Output: "1"
Explanation: This is the base case.
Example 2:
Input: n = 4
Output: "1211"
Explanation:
countAndSay(1) = "1"
countAndSay(2) = say "1" = one 1 = "11"
countAndSay(3) = say "11" = two 1's = "21"
countAndSay(4) = say "21" = one 2 + one 1 = "12" + "11" = "1211"
1 <= n <= 30Design and implement a production-grade CI/CD pipeline using GitHub Actions. The pipeline should handle the full lifecycle from code validation to deployment, with proper safeguards and optimizations.
Given a project configuration, generate a complete GitHub Actions workflow YAML that implements:
needs for dependenciesExample 1:
Input: {
language: "node",
testCmd: "npm test",
buildCmd: "npm run build",
deployTarget: "aws-ecs",
branches: { main: "production", develop: "staging" }
}
Output:
name: CI/CD Pipeline
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- run: npm ci
- run: npm run lint
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- run: npm ci
- run: npm test -- --coverage
...
deploy-staging:
needs: [lint, test, build]
if: github.ref == 'refs/heads/develop'
environment: staging
...
deploy-production:
needs: [lint, test, build]
if: github.ref == 'refs/heads/main'
environment: production
...
In this challenge, you will design a system that efficiently finds the top $K$ drivers within a certain radius of a given latitude and longitude. This is a core component of ride-hailing services like Uber or Lyft.
Given millions of drivers whose positions are constantly updating, how do you handle:
updateLocation(driverId: string, lat: number, lon: number) function.findNearbyDrivers(lat: number, lon: number, radius: number): string[] function.Input:
updateLocation("D1", 37.7749, -122.4194) (San Francisco)
findNearbyDrivers(37.7750, -122.4195, 1.0)
Output:
["D1"]
A car travels from a starting position to a target destination, which is target miles east of the starting position.
Along the way, there are gas stations. Each station[i] represents a gas station that is position_i miles east of the starting position, and has fuel_i liters of gas.
The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it. It uses 1 liter of gas per 1 mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car.
Return the minimum number of refueling stops the car must make in order to reach its destination. If it cannot reach the destination, return -1.
Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there. If the car reaches the destination with 0 fuel left, it is still considered to have arrived.
Example 1:
Input: target = 1, startFuel = 1, stations = []
Output: 0
Explanation: We can reach the target without refueling.
Example 2:
Input: target = 100, startFuel = 1, stations = [[10, 100]]
Output: -1
Explanation: We can't reach the first station even though there is 100 liters of gas there.
Example 3:
Input: target = 100, startFuel = 10, stations = [[10, 60], [20, 30], [30, 30], [60, 40]]
Output: 2
Explanation:
We start with 10 liters of fuel.
We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas.
Then, we drive from position 10 to position 60 (expending 50 liters of fuel),
and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target.
We made 2 refueling stops along the way, so we return 2.
1 <= target, startFuel <= 10^90 <= stations.length <= 500Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.
Example 1:
Input: head = [1,1,2]
Output: [1,2]
Example 2:
Input: head = [1,1,2,3,3]
Output: [1,2,3]
[0, 300].-100 <= Node.val <= 100Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.
You must write an algorithm that runs in O(n) time.
Example 1:
Input: nums = [100,4,200,1,3,2]
Output: 4
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
Example 2:
Input: nums = [0,3,7,2,5,8,4,6,0,1]
Output: 9
0 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^9In English, we have a concept called root, which can be followed by some other word to form another longer word - let's call this word derivative. For example, when the root "help" is followed by the word "ful", we can form a derivative "helpful".
Given a dictionary consisting of many roots and a sentence consisting of words separated by spaces, replace all the derivatives in the sentence with the root forming it. If a derivative can be replaced by more than one root, replace it with the root that has the shortest length.
Return the sentence after the replacement.
Example 1:
Input: dictionary = ["cat","bat","rat"], sentence = "the cattle was rattled by the battery"
Output: "the cat was rat by the bat"
Example 2:
Input: dictionary = ["a","b","c"], sentence = "aadsfasf absbs bbab cadsfafs"
Output: "a a b c"
1 <= dictionary.length <= 10001 <= dictionary[i].length <= 100dictionary[i] consists of only lowercase letters.1 <= sentence.length <= 10^5sentence consists of only lowercase letters and spaces.sentence is in the range [1, 1000].sentence is in the range [1, 1000].sentence will be separated by exactly one space.sentence does not have leading or trailing spaces.You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position.
Return true if you can reach the last index, or false otherwise.
Example 1:
Input: nums = [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:
Input: nums = [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, so you cannot reach the last index.
Example 3:
Input: nums = [0]
Output: true
Explanation: You are already at the last index.
1 <= nums.length <= 10^40 <= nums[i] <= 10^51 <= position_i < position_{i+1} < target1 <= fuel_i < 10^9