Find all anagrams of a search word in a list.
Function:
function findAnagrams(words: string[], searchWord: string): string[]
Example: words = ["enlist", "google", "inlets", "banana"] searchWord = "listen" Output: ["enlist", "inlets"]
Strategy:
Determine final state after asteroid collisions.
Function:
function asteroidCollision(asteroids: number[]): number[]
Examples:
Requirements:
Implement core BST operations and validation.
interface TreeNode {
val: number;
left: TreeNode | null;
right: TreeNode | null;
}
function insert(root: TreeNode | null, val: number): TreeNode
function isValidBST(root: TreeNode | null): boolean
Requirements:
- Left subtree values < node value
- Right subtree values > node value
- Handle duplicates
- Consider min/max constraints
Design a system to detect blocking words in reviews.
Requirements:
Implementation:
Find longest substring with same characters after k replacements.
function longestSubstring(s: string, k: number): number
Example:
Input: s = "AABABBA", k = 1
Output: 4
Explanation: Replace one 'B' to get "AAABBA"
Requirements:
Find all pairs with minimum difference in array.
function findMinDiffPairs(arr: number[]): [number, number][]
Example:
Input: [1, 5, 3, 19, 18, 25]
Output: [[18, 19]]
Requirements:
- Return all pairs with min difference
- Handle duplicates
- Sort for optimization
Implement a rate limiter for domain names.
Requirements:
class DomainRateLimiter {
constructor(limit: number, windowSeconds: number)
allowRequest(domain: string): boolean
}
Example:
1. a.com → pass
2. b.com → pass
3. a.com → pass
4. a.com → pass
5. a.com → drop (exceeded limit)
6. a.com → pass (after window)
Implementation:
Reverse a singly linked list.
interface ListNode {
val: number;
next: ListNode | null;
}
function reverseList(head: ListNode | null): ListNode | null
Constraints:
- 0 <= nodes <= 5000
- -5000 <= Node.val <= 5000
Example:
Input: [1,2,3,4,5]
Output: [5,4,3,2,1]
Find second largest distinct element in array.
function findSecondLargest(arr: number[]): number
Examples:
1. [35, 34, 23, 12] → 34
2. [10, 5, 10] → 5
3. [10, 10, 10] → -1 (no second largest)
Requirements:
- Handle duplicates
- Return -1 if no second largest
- Single pass solution preferred
Find shortest substring containing all distinct characters.
function shortestDistinctSubstring(s: string): string
Example:
Input: "aabcbcdbca"
Output: "dbca"
Requirements:
- Include all distinct chars
- Minimize length
- Handle duplicates
- Optimize runtime
Find shortest transformation sequence between words.
function ladderLength(
beginWord: string,
endWord: string,
wordList: string[]
): number
Examples:
1. beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log","cog"]
Output: 5 (hit→hot→dot→dog→cog)
2. endWord not in wordList
Output: 0
Constraints:
- 1 <= word.length <= 10
- All lowercase letters
- All words same length
- Unique words in wordList