319 challenges in the catalogue. Browse freely; sign in to open the editor and submit.
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
Notice that the solution set must not contain duplicate triplets.
Example 1:
Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
Explanation:
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
Example 2:
Input: nums = [0,1,1]
Output: []
Explanation: The only possible triplet does not sum up to 0.
Example 3:
Input: nums = [0,0,0]
Output: [[0,0,0]]
Explanation: The only possible triplet sums up to 0.
3 <= nums.length <= 3000-10^5 <= nums[i] <= 10^5Design a data structure that supports adding new words and finding if a string matches any previously added string.
Implement the WordDictionary class:
constructor() Initializes the object.addWord(word) Adds word to the data structure, it can be matched later.search(word) Returns true if there is any string in the data structure that matches word or false otherwise. word may contain dots '.' where dots can be matched with any letter.Example 1:
Input
["WordDictionary","addWord","addWord","addWord","search","search","search","search"]
[[],["bad"],["dad"],["mad"],["pad"],["bad"],[".ad"],["b.."]]
Output
[null,null,null,null,false,true,true,true]
Explanation
WordDictionary wordDictionary = new WordDictionary();
wordDictionary.addWord("bad");
wordDictionary.addWord("dad");
wordDictionary.addWord("mad");
wordDictionary.search("pad"); // return False
wordDictionary.search("bad"); // return True
wordDictionary.search(".ad"); // return True
wordDictionary.search("b.."); // return True
1 <= word.length <= 25word in addWord consists of lowercase English letters.word in search consist of . or lowercase English letters.2 dots in word for search queries.10^4 calls will be made to addWord and search.Design a highly scalable and reliable notification system that can deliver messages through various channels such as Push Notifications (iOS/Android), SMS, and Email.
Key components to include in your design:
Example Scenario:
Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].
The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
You must write an algorithm that runs in O(n) time and without using the division operation.
Example 1:
Input: nums = [1,2,3,4]
Output: [24,12,8,6]
Example 2:
Input: nums = [-1,1,0,-3,3]
Output: [0,0,9,0,0]
2 <= nums.length <= 10^5-30 <= nums[i] <= 30nums is guaranteed to fit in a 32-bit integer.Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis).
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,
F(0) = 0, F(1) = 1
F(n) = F(n - 1) + F(n - 2), for n > 1
Given n, calculate F(n).
Example 1:
Input: n = 2
Output: 1
Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.
Example 2:
Input: n = 3
Output: 2
Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.
Example 3:
Input: n = 4
Output: 3
Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.
0 <= n <= 30You are given an array prices where prices[i] is the price of a given stock on the ith day.
Find the maximum profit you can achieve. You may complete at most two transactions.
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
Example 1:
Input: prices = [3,3,5,0,0,3,1,4]
Output: 6
Explanation: Buy on day 4 (price=0) and sell on day 6 (price=3), profit=3.
Then buy on day 7 (price=1) and sell on day 8 (price=4), profit=3.
Total profit = 3 + 3 = 6.
Example 2:
Input: prices = [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1, sell on day 5, profit=4. One transaction is sufficient.
Example 3:
Input: prices = [7,6,4,3,1]
Output: 0
Explanation: No profitable transaction.
1 <= prices.length <= 10^50 <= prices[i] <= 10^5Given a pattern and a string s, find if s follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s.
Input: pattern = "abba", s = "dog cat cat dog"
Output: true
Input: pattern = "abba", s = "dog cat cat fish"
Output: false
Input: pattern = "aaaa", s = "dog cat cat dog"
Output: false
1 <= pattern.length <= 300pattern contains only lower-case English letters.1 <= s.length <= 3000s contains only lowercase English letters and spaces ' '.s does not contain any leading or trailing spaces.s are separated by a single space.Build a push notification processing system that handles notification delivery logic including channel filtering, quiet hours, and priority-based overrides.
interface Notification {
title: string;
body: string;
channel: string;
priority: "low" | "normal" | "high" | "urgent";
data?: Record<string, any>;
}
interface UserPreferences {
enabled: boolean;
channels: string[];
quietHours: { start: number; end: number };
}
interface NotificationResult {
shouldDeliver: boolean;
reason?: string;
notification?: Notification;
}
function processNotification(
notification: Notification,
preferences: UserPreferences,
currentHour: number
): NotificationResult
Input:
notification = { title: "Sale!", body: "50% off", channel: "promotions", priority: "high" }
preferences = { enabled: true, channels: ["promotions"], quietHours: { start: 22, end: 7 } }
currentHour = 10
Output:
{ shouldDeliver: true, notification: { title: "Sale!", body: "50% off", channel: "promotions", priority: "high" } }
Implement your own version of Array.prototype.map. The function takes an array and a callback, and returns a new array where each element is the result of calling the callback on the corresponding element of the input array.
function myMap<T, U>(arr: T[], callback: (value: T, index: number, array: T[]) => U): U[]
arr — The input array.callback — A function called for each element with (value, index, array).A new array containing the results of calling callback on each element.
myMap([1, 2, 3], x => x * 2); // [2, 4, 6]
myMap(['a', 'b', 'c'], (char, i) => `${i}:${char}`); // ['0:a', '1:b', '2:c']
myMap([], x => x); // []
(element, index, array) — all three argumentsImplement an EventEmitter class that supports subscribing to events, emitting events, and unsubscribing. This is a fundamental pattern used in Node.js, browser APIs, and many frameworks.
class EventEmitter {
on(event: string, callback: (...args: any[]) => void): void;
off(event: string, callback: (...args: any[]) => void): void;
emit(event: string, ...args: any[]): void;
once(event: string, callback: (...args: any[]) => void): void;
}
on(event, callback) — Subscribe a callback to an event. Multiple callbacks can be registered for the same event.off(event, callback) — Remove a specific callback from an event.emit(event, ...args) — Trigger all callbacks registered for the event, passing the arguments.once(event, callback) — Subscribe a callback that fires only once, then automatically unsubscribes.const emitter = new EventEmitter();
const handler = (msg: string) => console.log(msg);
emitter.on('greet', handler);
emitter.emit('greet', 'Hello!'); // logs "Hello!"
emitter.emit('greet', 'Hi!'); // logs "Hi!"
emitter.once('init', () => console.log('Initialized'));
emitter.emit('init'); // logs "Initialized"
emitter.emit('init'); // nothing happens
const handler = (x: number) => console.log(x * 2);
emitter.on('double', handler);
emitter.emit('double', 5); // logs 10
emitter.off('double', handler);
emitter.emit('double', 5); // nothing happens
off removes only the specific callback referenceemit calls listeners in the order they were registeredGiven the root of a binary tree, flatten the tree into a "linked list":
TreeNode class where the right child pointer points to the next node in the list and the left child pointer is always null.Example 1:
Input: root = [1,2,5,3,4,null,6]
Output: [1,null,2,null,3,null,4,null,5,null,6]
Example 2:
Input: root = []
Output: []
Example 3:
Input: root = [0]
Output: [0]
[0, 2000].-100 <= Node.val <= 100Can you flatten the tree in-place (O(1) extra space)?
Implement pow(x, n), which calculates x raised to the power n (i.e., x^n).
Input: x = 2.00000, n = 10
Output: 1024.00000
Input: x = 2.10000, n = 3
Output: 9.26100
Input: x = 2.00000, n = -2
Output: 0.25000
Explanation: 2^-2 = 1/2^2 = 1/4 = 0.25
-100.0 < x < 100.0-2^31 <= n <= 2^31 - 1n is an integer.-10^4 <= x^n <= 10^4(userId, messageCode, optionalData) to avoid duplicates.