You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tech-interview-handbook/contents/algorithms/array.md

102 lines
9.3 KiB

---
id: array
title: Array
---
7 years ago
## Notes
Is the array sorted or partially sorted? If it is, some form of binary search should be possible. This also usually means that the interviewer is looking for a solution that is faster than O(n).
7 years ago
Can you sort the array? Sometimes sorting the array first may significantly simplify the problem. Make sure that the order of array elements do not need to be preserved before attempting a sort.
For questions where summation or multiplication of a subarray is involved, pre-computation using hashing or a prefix/suffix sum/product might be useful.
If you are given a sequence and the interviewer asks for O(1) space, it might be possible to use the array itself as a hash table. For example, if the array only has values from 1 to N, where N is the length of the array, negate the value at that index (minus one) to indicate presence of that number.
## Arrays are sequences
Are there duplicate values in the array, would it affect the answer?
When using an index to iterate through array elements, be careful not to go out of bounds.
Be mindful about slicing or concatenating arrays in your code. Typically, slicing and concatenating arrays require O(n) time. Use start and end indices to demarcate a subarray/range where possible.
Sometimes you can traverse the array from the right rather than from the left.
Master the [sliding window technique](https://discuss.leetcode.com/topic/30941/here-is-a-10-line-template-that-can-solve-most-substring-problems) that applies to many subarray problems.
When you are given two arrays to process, it is common to have one index per array (pointer) to traverse/compare the both of them. For example, we use the same approach to merge two sorted arrays.
## Corner cases
7 years ago
- Empty sequence
- Sequence with 1 or 2 elements
- Sequence with repeated elements
## Recommended Leetcode questions
- [Two Sum](https://leetcode.com/problems/two-sum/)
- [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)
- [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/)
- [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/)
- [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/)
- [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/)
- [Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/)
- [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/)
- [3Sum](https://leetcode.com/problems/3sum/)
- [Container With Most Water](https://leetcode.com/problems/container-with-most-water/)
## More questions
### Easy
- Implement a circular buffer using an array. ([Solution](https://www.geeksforgeeks.org/circular-queue-set-1-introduction-array-implementation/))
- Given an array of integers, print out a histogram using the said array; include a base layer (all stars) ([Solution](https://www.geeksforgeeks.org/program-make-histogram-array/))
- Given an array and an index, find the product of the elements of the array except the element at that index. ([Solution](https://www.geeksforgeeks.org/a-product-array-puzzle/))
- Given 2 separate arrays, write a method to find the values that exist in both arrays and return them. ([Solution](https://www.geeksforgeeks.org/union-and-intersection-of-two-sorted-arrays-2/))
- Given an input array and another array that describes a new index for each element, mutate the input array so that each element ends up in their new index ([Solution](https://www.geeksforgeeks.org/reorder-a-array-according-to-given-indexes/))
- Discuss the runtime of the algorithm and how you can be sure there would not be any infinite loops
- Given an array of non-negative numbers, find continuous subarray with sum to S. ([Solution 1](https://www.geeksforgeeks.org/find-subarray-with-given-sum/)) ([Solution 2](http://blog.gainlo.co/index.php/2016/06/01/subarray-with-given-sum/))
- Given an array of numbers list out all triplets that sum to 0. Do so with a running time of less than O(n^3). ([Solution 1](https://www.geeksforgeeks.org/find-triplets-array-whose-sum-equal-zero/)) ([Solution 2](http://blog.gainlo.co/index.php/2016/07/19/3sum/))
- Given an array of numbers list out all quadruplets that sum to 0. Do so with a running time of less than O(n^4). ([Solution](https://www.geeksforgeeks.org/find-four-numbers-with-sum-equal-to-given-sum/))
- Given an array of integers, find the subarray with the largest sum. ([Solution](https://www.geeksforgeeks.org/largest-sum-contiguous-subarray/))
- Find the second maximum value in an array. ([Solution](https://www.geeksforgeeks.org/find-second-largest-element-array/))
- Rotate an array by an offset of k. ([Solution](https://www.geeksforgeeks.org/array-rotation/))
- Remove duplicates in an unsorted array where the duplicates are at a distance of k or less from each other. ([Solution](https://www.geeksforgeeks.org/check-given-array-contains-duplicate-elements-within-k-distance/))
- Given an unsorted list of integers, return true if the list contains any duplicates within k indices of each element. Do it faster than O(n^2). ([Solution](https://www.geeksforgeeks.org/check-given-array-contains-duplicate-elements-within-k-distance/))
- Given an unsorted list of integers, return true if the list contains any fuzzy duplicates within k indices of each element. A fuzzy duplicate is another integer within d of the original integer. Do it faster than O(n^2).
7 years ago
- E.g. If d = 4, then 6 is a fuzzy duplicate of 3 but 8 is not.
- Say you have an unordered list of numbers ranging from 1 to n, and one of the numbers is removed, how do you find that number? What if two numbers are removed? ([Solution](https://www.geeksforgeeks.org/find-the-missing-number/))
- Given an array of string, find the duplicated elements. ([Solution 1](https://www.geeksforgeeks.org/find-duplicates-in-on-time-and-constant-extra-space/)) ([Solution 2](http://blog.gainlo.co/index.php/2016/05/10/duplicate-elements-of-an-array/))
- Given an array of integers, find a maximum sum of non-adjacent elements. ([Solution 1](https://www.geeksforgeeks.org/maximum-sum-such-that-no-two-elements-are-adjacent/)) ([Solution 2](http://blog.gainlo.co/index.php/2016/12/02/uber-interview-question-maximum-sum-non-adjacent-elements/))
7 years ago
- E.g. `[1, 0, 3, 9, 2]` should return `10 (1 + 9)`.
- Given an array of integers, modify the array by moving all the zeroes to the end (right side). The order of other elements doesn't matter. ([Solution 1](https://www.geeksforgeeks.org/move-zeroes-end-array/)) ([Solution 2](http://blog.gainlo.co/index.php/2016/11/18/uber-interview-question-move-zeroes/))
7 years ago
- E.g. `[1, 2, 0, 3, 0, 1, 2]`, the program can output `[1, 2, 3, 1, 2, 0, 0]`.
- Given an array, return the length of the longest increasing contiguous subarray. ([Solution 1](https://www.geeksforgeeks.org/longest-increasing-subarray/)) ([Solution 2](http://blog.gainlo.co/index.php/2017/02/02/uber-interview-questions-longest-increasing-subarray/))
- E.g., `[1, 3, 2, 3, 4, 8, 7, 9]`, should return `4` because the longest increasing array is `[2, 3, 4, 8]`
- Given an array of integers where every value appears twice except one, find the single, non-repeating value. Follow up: do so with O(1) space. ([Solution](https://www.geeksforgeeks.org/find-element-appears-array-every-element-appears-twice/))
- E.g., `[2, 5, 3, 2, 1, 3, 4, 5, 1]` returns 4, because it is the only value that appears in the array only once.
### Medium
- Given a list of item prices, find all possible combinations of items that sum a particular value `K`. ([Solution](https://www.geeksforgeeks.org/combinational-sum/))
- Given an array of integers find whether there is a sub-sequence that sums to 0 and return it. ([Solution](https://www.geeksforgeeks.org/find-if-there-is-a-subarray-with-0-sum/))
- E.g. `[1, 2, -3, 1]` => `[1, 2, -3]` or `[2, -3, 1]`
- Trapping rain water: You have an array with the heights of an island (at point 1, point 2 etc) and you want to know how much water would remain on this island (without flowing away). ([Solution](https://www.geeksforgeeks.org/trapping-rain-water/))
### Hard
- Given a set of rectangles represented by a height and an interval along the y-axis, determine the size of its union. ([Solution](https://www.geeksforgeeks.org/divide-and-conquer-set-7-the-skyline-problem/))
- Given an array, find the longest arithmetic progression. ([Solution](https://www.geeksforgeeks.org/longest-arithmetic-progression-dp-35/))
- Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
- E.g. `[100, 4, 200, 1, 3, 2] => 4`. Explanation: The longest consecutive elements sequence is `[1, 2, 3, 4]`. Therefore its length is 4. Note: Your algorithm should run in O(n) complexity. ([Solution](https://www.geeksforgeeks.org/longest-consecutive-subsequence/))
### Other
- In an array of arrays, e.g. given `[[], [1, 2, 3], [4, 5], [], [], [6, 7], [8], [9, 10], [], []]`, print: `1, 2, 3, 4, 5, 6, 7, 8, 9, 10`.
- Implement an iterator that supports `hasNext()`, `next()` and `remove()` methods.
- Paginate an array with constraints, such as skipping certain items.
- Given array of arrays, sort them in ascending order.
- Given an array containing only digits `0-9`, add one to the number and return the array.
- E.g. Given `[1, 4, 2, 1]` which represents `1421`, return `[1, 4, 2, 2]` which represents `1422`.