Hard Practice Problems
11 hard problems out of 40 total. Push your limits with advanced problem-solving.
Top N Frequent Words
hardWrite a function `topNFrequentWords` that takes a large text document as input and returns the top N most frequent words. The function should be optimized for performance. ## Function Signature ```function topNFrequentWords(text, N);``` ### Example 1: > **Input:** `text = "the quick brown fox jumps over the lazy dog the the the quick quick"`, `N = 2` \ > **Output:** `["the", "quick"]` \ > **Explanation:** "the" and "quick" are the most frequent words appearing 4 and 3 times respectively. ### Example 2: > **Input:** `text = "a a a b b c"`, `N = 1` \ > **Output:** `["a"]` \ > **Explanation:** "a" is the most frequent word appearing 3 times. ### Example 3: > **Input:** `text = "apple banana apple banana orange apple banana banana"`, `N = 2` \ > **Output:** `["banana", "apple"]` \ > **Explanation:** "banana" and "apple" are the most frequent words appearing 4 and 3 times respectively.
StringSort Colors
hardWrite a function `sortColors` that takes an array of integers `nums` representing colors, where: - 0 represents red, - 1 represents white, - 2 represents blue. The function should sort the array in place so that the colors are in the order red, white, and blue. ## Function Signature ```function sortColors(nums);``` ### Example 1: > **Input:** `nums = [2,0,2,1,1,0]` \ > **Output:** `[0,0,1,1,2,2]` \ > **Explanation:** The array is sorted so that all reds come first, followed by whites, and then blues. ### Example 2: > **Input:** `nums = [2,0,1]` \ > **Output:** `[0,1,2]` \ > **Explanation:** The array is sorted so that all reds come first, followed by whites, and then blues. o solve it in a single pass (O(n) time complexity) using constant space (O(1) extra space). ### Example 3: > **Input:** `nums = [1,2,0,2,1,0]` \ > **Output:** `[0,0,1,1,2,2]` \ > **Explanation:** The array is sorted so that all reds come first, followed by whites, and then blues. ### Example 4: > **Input:** `nums = [0]` \ > **Output:** `[0]` \ > **Explanation:** The array contains only one element which is already sorted.
SortingLongest Palindromic Substring
hardWrite a function `longestPalindrome` that takes a string and returns the longest palindromic substring. ## Function Signature ```function longestPalindrome(s);``` ### Example 1: > **Input:** `s = "babad"` \ > **Output:** `"bab"` \ > **Explanation:** The longest palindromic substring is "bab". ### Example 2: > **Input:** `s = "cbbd"` \ > **Output:** `"bb"` \ > **Explanation:** The longest palindromic substring is "bb". ### Example 3: > **Input:** `s = "a"` \ > **Output:** `"a"` \ > **Explanation:** The longest palindromic substring is "a".
StringSearch in Rotated Sorted Array
hardWrite a function `search` that takes a rotated sorted array and a target value, and returns the index if the target is found. If not, returns -1. ## Function Signature ```function search(nums, target);``` ### Example 1: > **Input:** `nums = [4,5,6,7,0,1,2]`, `target = 0` \ > **Output:** `4` \ > **Explanation:** Target 0 is at index 4. ### Example 2: > **Input:** `nums = [4,5,6,7,0,1,2]`, `target = 3` \ > **Output:** `-1` \ > **Explanation:** Target 3 is not in the array.
ArrayTop K Frequent Elements
hardWrite a function `topKFrequent` that takes an array of integers and an integer k, and returns the k most frequent elements. ## Function Signature ```function topKFrequent(nums, k);``` ### Example 1: > **Input:** `nums = [1,1,1,2,2,3]`, `k = 2` \ > **Output:** `[1,2]` \ > **Explanation:** The two most frequent elements are 1 and 2. ### Example 2: > **Input:** `nums = [1]`, `k = 1` \ > **Output:** `[1]` \ > **Explanation:** The most frequent element is 1.
ArrayPermutations
hardWrite a function `permute` that takes an array of distinct integers and returns all possible permutations. ## Function Signature ```function permute(nums);``` ### Example 1: > **Input:** `nums = [1,2,3]` \ > **Output:** `[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]` \ > **Explanation:** The permutations of [1,2,3] are [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]. ### Example 2: > **Input:** `nums = [0,1]` \ > **Output:** `[[0,1],[1,0]]` \ > **Explanation:** The permutations of [0,1] are [[0,1],[1,0]]. ### Example 3: > **Input:** `nums = [1]` \ > **Output:** `[[1]]` \ > **Explanation:** The permutations of [1] are [[1]].
ArraySubsets
hardWrite a function `subsets` that takes an array of distinct integers and returns all possible subsets. ## Function Signature ```function subsets(nums);``` ### Example 1: > **Input:** `nums = [1,2,3]` \ > **Output:** `[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]` \ > **Explanation:** The subsets are [[] [1] [2] [1,2] [3] [1,3] [2,3] [1,2,3]]. ### Example 2: > **Input:** `nums = [0]` \ > **Output:** `[[],[0]]` \ > **Explanation:** The subsets are [[] [0]].
ArrayWord Break
hardWrite a function `wordBreak` that takes a string s and a dictionary of strings wordDict, and returns true if s can be segmented into a space-separated sequence of one or more dictionary words. ## Function Signature ```function wordBreak(s, wordDict);``` ### Example 1: > **Input:** `s = "bigdevsoon"`, `wordDict = ["big","dev","soon"]` \ > **Output:** `true` \ > **Explanation:** The string "bigdevsoon" can be segmented as "big dev soon". ### Example 2: > **Input:** `s = "applepenapple"`, `wordDict = ["apple","pen"]` \ > **Output:** `true` \ > **Explanation:** The string "applepenapple" can be segmented as "apple pen apple". ### Example 3: > **Input:** `s = "catsandog"`, `wordDict = ["cats","dog","sand","and","cat"]` \ > **Output:** `false` \ > **Explanation:** The string "catsandog" cannot be segmented as a space-separated sequence of one or more dictionary words.
StringFind All Duplicates in an Array
hardGiven an array of integers, find all elements that appear twice. ## Function Signature ```function findDuplicates(nums);``` ### Example 1: > **Input:** `nums = [4,3,2,7,8,2,3,1]` \ > **Output:** `[2,3]` \ > **Explanation:** The numbers 2 and 3 appear twice in the array. ### Example 2: > **Input:** `nums = [1,1,2,3,3,4,4,4,5]` \ > **Output:** `[1,3,4]` \ > **Explanation:** The numbers 1, 3, and 4 appear twice or more in the array.
ArrayLongest Substring Without Repeating Characters
hardFind the length of the longest substring without repeating characters. ## Function Signature ```function lengthOfLongestSubstring(s);``` ### Example 1: > **Input:** `s = "abcabcbb"` \ > **Output:** `3` \ > **Explanation:** The longest substring without repeating characters is "abc". ### Example 2: > **Input:** `s = "bbbbb"` \ > **Output:** `1` \ > **Explanation:** The longest substring without repeating characters is "b" of length 1.
StringDivide Two Integers
hardDivide two integers without using multiplication, division, and mod operator. ## Function Signature ```function divide(dividend, divisor);``` ### Example 1: > **Input:** `dividend = 10, divisor = 3` \ > **Output:** `3` \ > **Explanation:** The quotient of 10/3 is 3. ### Example 2: > **Input:** `dividend = 7, divisor = -3` \ > **Output:** `-2` \ > **Explanation:** The quotient of 7/-3 is -2.
MathReady to practice?
Solve all 40 problems in our built-in code editor with instant feedback.
Start 7-Day Free Trial