Medium Practice Problems
15 medium problems out of 40 total. Build confidence with intermediate challenges.
Merge Intervals
mediumWrite a function `mergeIntervals` that takes an array of intervals and merges all overlapping intervals. ## Function Signature ```function mergeIntervals(intervals);``` ### Example 1: > **Input:** `intervals = [[1, 3], [2, 6], [8, 10], [15, 18]]` \ > **Output:** `[[1, 6], [8, 10], [15, 18]]` \ > **Explanation:** Since intervals [1, 3] and [2, 6] overlap, merge them into [1, 6]. ### Example 2: > **Input:** `intervals = [[1, 4], [4, 5]]` \ > **Output:** `[[1, 5]]` \ > **Explanation:** Intervals [1, 4] and [4, 5] are considered overlapping.
ArrayBest Time to Buy and Sell Stock
mediumWrite a function `maxProfit` that takes an array where the `i-th` element is the price of a given stock on day `i`, and returns the maximum profit you can achieve by buying and selling one share of the stock. ## Function Signature ```function maxProfit(prices);``` ### Example 1: > **Input:** `prices = [7,1,5,3,6,4]` \ > **Output:** `5` \ > **Explanation:** Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. ### Example 2: > **Input:** `prices = [7,6,4,3,1]` \ > **Output:** `0` \ > **Explanation:** In this case, no transactions are done, i.e. max profit = 0.
ArrayGroup Anagrams
mediumWrite a function `groupAnagrams` that takes an array of strings and groups the anagrams together. ## Function Signature ```function groupAnagrams(strs);``` ### Example 1: > **Input:** `strs = ["eat", "tea", "tan", "ate", "nat", "bat"]` \ > **Output:** `[["eat", "tea", "ate"], ["tan", "nat"], ["bat"]]` \ > **Explanation:** All strings that are anagrams are grouped together. ### Example 2: > **Input:** `strs = [""]` \ > **Output:** `[[""]]` \ > **Explanation:** An empty string is considered an anagram of itself. ### Example 3: > **Input:** `strs = ["a"]` \ > **Output:** `[["a"]]` \ > **Explanation:** A single character string is considered an anagram of itself.
StringMerge Sorted Lists
mediumWrite a function `mergeSortedLists` that takes two sorted linked lists and merges them into one sorted list. ## Function Signature ```function mergeSortedLists(l1, l2);``` ### Example 1: > **Input:** `l1 = [1, 2, 4]`, `l2 = [1, 3, 4]` \ > **Output:** `[1, 1, 2, 3, 4, 4]` \ > **Explanation:** The merged list is sorted. ### Example 2: > **Input:** `l1 = []`, `l2 = []` \ > **Output:** `[]` \ > **Explanation:** Merging two empty lists results in an empty list. ### Example 3: > **Input:** `l1 = []`, `l2 = [0]` \ > **Output:** `[0]` \ > **Explanation:** Merging an empty list with a non-empty list results in the non-empty list.
Data StructureThree Sum
mediumWrite a function `threeSum` that takes an array of integers and returns all unique triplets that sum up to zero. ## Function Signature ```function threeSum(nums);``` ### Example 1: > **Input:** `nums = [-1, 0, 1, 2, -1, -4]` \ > **Output:** `[[-1, -1, 2], [-1, 0, 1]]` \ > **Explanation:** The triplets that sum up to zero are [-1, -1, 2] and [-1, 0, 1]. ### Example 2: > **Input:** `nums = []` \ > **Output:** `[]` \ > **Explanation:** No triplets can be formed from an empty array. ### Example 3: > **Input:** `nums = [0]` \ > **Output:** `[]` \ > **Explanation:** No triplets can be formed from a single element.
ArrayProduct of Array Except Self
mediumWrite a function `productExceptSelf` that takes an array of integers and returns an array such that each element at index `i` is the product of all the numbers in the original array except the one at `i`. ## Function Signature ```function productExceptSelf(nums);``` ### Example 1: > **Input:** `nums = [1, 2, 3, 4]` \ > **Output:** `[24, 12, 8, 6]` \ > **Explanation:** The product of all the numbers except the one at each index is [24, 12, 8, 6]. ### Example 2: > **Input:** `nums = [-1, 1, 0, -3, 3]` \ > **Output:** `[0, 0, 9, 0, 0]` \ > **Explanation:** The product of all the numbers except the one at each index is [0, 0, 9, 0, 0].
ArrayFirst Missing Positive
mediumWrite a function `firstMissingPositive` that takes an array of integers and finds the smallest missing positive integer. ## Function Signature ```function firstMissingPositive(nums);``` ### Example 1: > **Input:** `nums = [1,2,0]` \ > **Output:** `3` \ > **Explanation:** The smallest missing positive integer is 3. ### Example 2: > **Input:** `nums = [3,4,-1,1]` \ > **Output:** `2` \ > **Explanation:** The smallest missing positive integer is 2. ### Example 3: > **Input:** `nums = [7,8,9,11,12]` \ > **Output:** `1` \ > **Explanation:** The smallest missing positive integer is 1.
ArrayContainer With Most Water
mediumWrite a function `maxArea` that takes an array of integers where each element represents the height of a vertical line, and returns the maximum area of water it can contain. ## Function Signature ```function maxArea(height);``` ### Example 1: > **Input:** `height = [1,8,6,2,5,4,8,3,7]` \ > **Output:** `49` \ > **Explanation:** The maximum area is formed between the lines at index 1 and 8, resulting in an area of 49. ### Example 2: > **Input:** `height = [1,1]` \ > **Output:** `1` \ > **Explanation:** The maximum area is formed between the lines at index 0 and 1, resulting in an area of 1.
ArrayLongest Consecutive Sequence
mediumWrite a function `longestConsecutive` that takes an array of integers and returns the length of the longest consecutive elements sequence. ## Function Signature ```function longestConsecutive(nums);``` ### 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` \ > **Explanation:** The longest consecutive elements sequence is [0, 1, 2, 3, 4, 5, 6, 7, 8]. Therefore, its length is 9.
ArrayFind Peak Element
mediumWrite a function `findPeakElement` that takes an array of integers and finds a peak element, and returns its index. A peak element is an element that is greater than its neighbors. ## Function Signature ```function findPeakElement(nums);``` ### Example 1: > **Input:** `nums = [1,2,3,1]` \ > **Output:** `2` \ > **Explanation:** 3 is a peak element and its index is 2. ### Example 2: > **Input:** `nums = [1,2,1,3,5,6,4]` \ > **Output:** `5` \ > **Explanation:** 6 is a peak element and its index is 5.
ArrayMaximum Subarray
mediumWrite a function `maxSubArray` that takes an array of integers and returns the contiguous subarray (containing at least one number) which has the largest sum. ## Function Signature ```function maxSubArray(nums);``` ### Example 1: > **Input:** `nums = [-2,1,-3,4,-1,2,1,-5,4]` \ > **Output:** `6` \ > **Explanation:** The subarray [4,-1,2,1] has the largest sum = 6. ### Example 2: > **Input:** `nums = [1]` \ > **Output:** `1` \ > **Explanation:** The largest sum subarray is [1]. ### Example 3: > **Input:** `nums = [5,4,-1,7,8]` \ > **Output:** `23` \ > **Explanation:** The subarray [5,4,-1,7,8] has the largest sum = 23.
ArrayString Compression
mediumCompress a string using the counts of repeated characters. ## Function Signature ```function compressString(chars);``` ### Example 1: > **Input:** `chars = ["a","a","b","b","c","c","c"]` \ > **Output:** `["a","2","b","2","c","3"]` \ > **Explanation:** The string compression of "aabbbccc" is "a2b2c3". ### Example 2: > **Input:** `chars = ["a","b","c"]` \ > **Output:** `["a","b","c"]` \ > **Explanation:** The string compression of "abc" is "abc" as each character appears only once.
StringValid Anagram
mediumDetermine if two strings are anagrams of each other. ## Function Signature ```function isAnagram(s, t);``` ### Example 1: > **Input:** `s = "anagram", t = "nagaram"` \ > **Output:** `true` \ > **Explanation:** The strings "anagram" and "nagaram" are anagrams of each other. ### Example 2: > **Input:** `s = "rat", t = "car"` \ > **Output:** `false` \ > **Explanation:** The strings "rat" and "car" are not anagrams.
StringHappy Number
mediumDetermine if a number is 'happy' which is defined by repeatedly replacing the number with the sum of the squares of its digits, until it is one. ## Function Signature ```function isHappy(n);``` ### Example 1: > **Input:** `n = 19` \ > **Output:** `true` \ > **Explanation:** 19 is a happy number. ### Example 2: > **Input:** `n = 2` \ > **Output:** `false` \ > **Explanation:** 2 is not a happy number.
MathLength of Last Word
mediumCalculate the length of the last word in a string separated by space characters. ## Function Signature ```function lengthOfLastWord(s);``` ### Example 1: > **Input:** `s = "Hello World"` \ > **Output:** `5` \ > **Explanation:** The length of the last word "World" is 5. ### Example 2: > **Input:** `s = " fly me to the moon "` \ > **Output:** `4` \ > **Explanation:** The length of the last word "moon" is 4.
StringReady to practice?
Solve all 40 problems in our built-in code editor with instant feedback.
Start 7-Day Free Trial