Ready to level up a bit?
What's a linear data structure that holds a fixed number of containers to store data? Arrays. And let's work through lots of array problems, but we'll up the ante and make them a little more difficult!
How do I use this section?
We have an array of length 2 * n (even length) that consists of random integers. [1, 3, 2, 6, 5, 4] We are asked to create pairs out of these integers, like such: [[1, 3], [2, 6], [5, 4]] How can we divide up the pairs such that the sum of smaller integers in each pair is maximized? <img src="https://storage.goo...
Assume we're given an unsorted array of numbers such as this: [ 2, 5, 1, 4, 9, 6, 3, 7 ] We are told that when this array is sorted, there is a series of n consecutive numbers. You are given a lower bound and an upper bound for this sequence. There is one consecutive number missing, and we need to find it. As an exa...
We're given an array of continuous numbers that should increment sequentially by 1, which just means that we expect a sequence like: [1, 2, 3, 4, 5, 6, 7] However, we notice that there are some missing numbers in the sequence. [1, 2, 4, 5, 7] <img src="https://storage.googleapis.com/algodailyrandomassets/curricul...
You're given a number n. Can you write a method sumOfAllPrimes that finds all prime numbers smaller than or equal to n, and returns a sum of them? For example, we're given the number 15. All prime...
We're given an array of integers that looks like the following: `js [0, 1, 2, 3, 4, 5, 6, 7, 8] ` Let's imagine we're an assembly line and we decide to shift some workers around. Say we take our array, and rotate it at a random pivot so that the section after the pivot comes before. Let's put our pivot between 5 an...
Given an array, return another array with just the ordered unique elements from the given array. In other words, you're removing any duplicates. Note: Order needs to be preserved, so no sorting should be done. And the order should be maintained with the first occurrence of the element in the given array. <img src="https://sto...
Given an array of numbers, return true if there is a subarray that sums up to a certain number n. A subarray is a contiguous subset of the array. For example the subarray of [1,2,3,4,5] is [1,2,3] or [3,4,5] or [2,3,4] etc. `js const arr = [1, 2, 3], sum = 5 subarraySum(arr, sum) // true // [2, 3] sum up to...
Say we are given an integer array of an even length, where different numbers in the array represent certain kinds of snacks or treats. Each number maps to, or represents, one kind of snack. So the following array would have two kinds: snack type 3 and type 2: `js const snacks = [3, 3, 2, 2]; ` <img src="https://stor...
We have an unsorted array of integers such as the following: [0, 3, -1, -2, 1] if (len == 2) return Math.Max(nums[0], nums[1]); In the above example, the minimum number is -2 and the maximum is 3. If it were sorted, it would look like: [-2, -1, 0, 1, 3] if (len == 2) return Math.Max(nu...
If we are given an array of integers, can you return an output array such that each corresponding input's elements returns the product of the input array except itself? This can be hard to explain, s...
This is a classic and very common interview problem. Given an array of integers, return the indices of the two numbers in it that add up to a specific "goal" number. Suppose we had an array [1, 3, 6, 7, 9], and let's say our "goal" number was 10. Our numbers to sum to it could be 3 and 7, and we would return an array of in...
Given two strings, one named sub and the other str, determine if sub is a subsequence of str. `js const str = "barbell" const sub = "bell" isASubsequence(sub, str); // true ` For the sake of the exercise, let's assume that these strings only have lower case characters. <img src="https://storage.googleapis....
This is a classic technical interview question that I've seen a number of times in real life interviews. Let's say you're given an array of stock prices, with each element being an integer that represents a price in dollars. ` [ 10, 7, 6, 2, 9, 4 ] ` Each index of the array represents a single day, and the the ele...
We have an array that model square blocks on a city street. On each block is a non-negative whole number. Ever heard of hopscotch? Picture that as an array: [1, 3, 2, 2, 1] Now, in hopscotch, you nee...
We're given a set in the form of an array with a unique integers. Can you write a function that tells us whether it can be separated into two subsets whose elements have equal sums? Here's an example: [5, 9, 7, 21]: <img src="https://storage.googleapis.com/algodailyrandomassets/curriculum/medium-arrays/split-sets-to-equal-sub...
Assume that we are building software to determine how many planes are in the sky. The data that we have on planes currently in the air comes in the form of a grid, modeled by a...
This is a classic interview question that is used in a lot in technical interviews, frequently in the form of a calendar or meeting management application. As such, we'll also assume the same use case, as it's easier to visualize. <img src="https://storage.googleapis.com/algodailyrandomassets/curriculum/merge_intervals/0...
Write a method that moves all zeros in an array to its end. You should maintain the order of all other elements. Here's an example: `js zerosToEnd([1, 0, 2, 0, 4, 0]) // [1, 2, 4, 0, 0, 0] ` <img src="https://storage.googleapis.com/algodailyrandomassets/curriculum/medium-arrays/zeros-to-the-end-cover-image.png" class="...
A circular array is one where the next element of the last element is the first element. You know how standard arrays look. Instead of [1, 2, 3, 4], imagine the following, where after index 7, we'd move back to index 0. <img src="https://storage.googleapis.com/algodaily...
We're given a list or array of numbers, like the following: const nums = [5, 16, 7, 9, -1, 4, 3, 11, 2] Can you write an algorithm to find the k largest values in a list of n elements? If k were 3, we'd want the three largest numbers returned. The correct logic would return [16, 11, 9]. Order is not a consideration....
You are given an array of points where each point represents a point on the X-Y plane points[i] = [xi, yi]. You are also given an integer n. Find the n closest points to the origin (0, 0). Your answer should be in descending order of distance (largest to smallest). The answer is guarant...
You are given an array of positive integers w where w[i] describes the weight of the ith index. Implement the function randomPick(), which randomly picks an index in the range [0, w.length - 1] (inclusive) and returns it. The probability of picking an element is w[i] / sum(w), where i is the in...