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.
140 lines
8.9 KiB
140 lines
8.9 KiB
5 years ago
|
---
|
||
|
id: array
|
||
3 years ago
|
title: Array cheatsheet for coding interviews
|
||
|
description: Array study guide for coding interviews, including practice questions, techniques, time complexity, and recommended resources
|
||
|
keywords:
|
||
|
[
|
||
|
array coding interview study guide,
|
||
|
array tips for coding interviews,
|
||
|
array practice questions,
|
||
|
array useful techniques,
|
||
|
array time complexity,
|
||
|
array recommended study resources,
|
||
|
]
|
||
|
sidebar_label: Array
|
||
3 years ago
|
toc_max_heading_level: 2
|
||
5 years ago
|
---
|
||
7 years ago
|
|
||
3 years ago
|
<head>
|
||
|
<meta property="og:image" content="https://www.techinterviewhandbook.org/social/algorithms/algorithms/algorithms-array.png" />
|
||
|
</head>
|
||
|
|
||
3 years ago
|
## Introduction
|
||
5 years ago
|
|
||
3 years ago
|
Arrays hold values of the same type at contiguous memory locations. In an array, we're usually concerned about two things - the position/index of an element and the element itself. Different programming languages implement arrays under the hood differently and can affect the time complexity of operations you make to the array. In some languages like Python, JavaScript, Ruby, PHP, the array (or list in Python) size is dynamic and you do not need to have a size defined beforehand when creating the array. As a result, people usually have an easier time using these languages for interviews.
|
||
|
|
||
|
Arrays are among the most common data structures encountered during interviews. Questions which ask about other topics would likely involve arrays/sequences as well. Mastery of array is essential for interviews!
|
||
7 years ago
|
|
||
3 years ago
|
**Advantages**
|
||
5 years ago
|
|
||
3 years ago
|
- Store multiple elements of the same type with one single variable name
|
||
|
- Accessing elements is fast as long as you have the index, as opposed to [linked lists](./linked-list.md) where you have to traverse from the head.
|
||
5 years ago
|
|
||
3 years ago
|
**Disadvantages**
|
||
5 years ago
|
|
||
3 years ago
|
- Addition and removal of elements into/from the middle of an array is slow because the remaining elements need to be shifted to accommodate the new/missing element. An exception to this is if the position to be inserted/removed is at the end of the array.
|
||
|
- For certain languages where the array size is fixed, it cannot alter its size after initialization. If an insertion causes the total number of elements to exceed the size, a new array has to be allocated and the existing elements have to be copied over. The act of creating a new array and transferring elements over takes O(n) time.
|
||
4 years ago
|
|
||
3 years ago
|
## Learning resources
|
||
|
|
||
|
- Readings
|
||
|
- [Array in Data Structure: What is, Arrays Operations](https://www.guru99.com/array-data-structure.html), Guru99
|
||
|
- Videos
|
||
|
- [Arrays](https://www.coursera.org/lecture/data-structures/arrays-OsBSF), University of California San Diego
|
||
|
|
||
|
## Common terms
|
||
5 years ago
|
|
||
3 years ago
|
Common terms you see when doing problems involving arrays:
|
||
5 years ago
|
|
||
3 years ago
|
- Subarray - A range of contiguous values within an array.
|
||
|
- Example: given an array `[2, 3, 6, 1, 5, 4]`, `[3, 6, 1]` is a subarray while `[3, 1, 5]` is not a subarray.
|
||
|
- Subsequence - A sequence that can be derived from the given sequence by deleting some or no elements without changing the order of the remaining elements.
|
||
|
- Example: given an array `[2, 3, 6, 1, 5, 4]`, `[3, 1, 5]` is a subsequence but `[3, 5, 1]` is not a subsequence.
|
||
5 years ago
|
|
||
3 years ago
|
## Time complexity
|
||
5 years ago
|
|
||
3 years ago
|
| Operation | Big-O | Note |
|
||
|
| --- | --- | --- |
|
||
|
| Access | O(1) | |
|
||
|
| Search | O(n) | |
|
||
|
| Search (sorted array) | O(log(n)) | |
|
||
|
| Insert | O(n) | Insertion would require shifting all the subsequent elements to the right by one and that takes O(n) |
|
||
|
| Insert (at the end) | O(1) | Special case of insertion where no other element needs to be shifted |
|
||
|
| Remove | O(n) | Removal would require shifting all the subsequent elements to the left by one and that takes O(n) |
|
||
|
| Remove (at the end) | O(1) | Special case of removal where no other element needs to be shifted |
|
||
5 years ago
|
|
||
3 years ago
|
## Things to look out for during interviews
|
||
5 years ago
|
|
||
3 years ago
|
- Clarify if there are duplicate values in the array. Would the presence of duplicate values affect the answer? Does it make the question simpler or harder?
|
||
|
- 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 would take O(n) time. Use start and end indices to demarcate a subarray/range where possible.
|
||
5 years ago
|
|
||
|
## Corner cases
|
||
7 years ago
|
|
||
5 years ago
|
- Empty sequence
|
||
|
- Sequence with 1 or 2 elements
|
||
|
- Sequence with repeated elements
|
||
3 years ago
|
- Duplicated values in the sequence
|
||
|
|
||
|
## Techniques
|
||
|
|
||
|
Note that because both arrays and strings are sequences (a string is an array of characters), most of the techniques here will apply to string problems.
|
||
|
|
||
|
### Sliding window
|
||
|
|
||
|
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/substring problems. In a sliding window, the two pointers usually move in the same direction will never overtake each other. This ensures that each value is only visited at most twice and the time complexity is still O(n). Examples: [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/), [Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/), [Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/)
|
||
|
|
||
|
### Two pointers
|
||
|
|
||
|
Two pointers is a more general version of sliding window where the pointers can cross each other and can be on different arrays. Examples: [Sort Colors](https://leetcode.com/problems/sort-colors/), [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/)
|
||
|
|
||
|
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, incrementing one of the pointers when relevant. For example, we use this approach to merge two sorted arrays. Examples: [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/)
|
||
|
|
||
|
### Traversing from the right
|
||
|
|
||
|
Sometimes you can traverse the array starting from the right instead of the conventional approach of from the left. Examples: [Daily Temperatures](https://leetcode.com/problems/daily-temperatures/), [Number of Visible People in a Queue](https://leetcode.com/problems/number-of-visible-people-in-a-queue/)
|
||
|
|
||
|
### Sorting the array
|
||
|
|
||
|
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).
|
||
|
|
||
|
Can you sort the array? Sometimes sorting the array first may significantly simplify the problem. Obviously this would not work if the order of array elements need to be preserved. Examples: [Merge Intervals](https://leetcode.com/problems/merge-intervals/), [Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/)
|
||
|
|
||
|
### Precomputation
|
||
|
|
||
|
For questions where summation or multiplication of a subarray is involved, pre-computation using hashing or a prefix/suffix sum/product might be useful. Examples: [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/), [Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/), [LeetCode questions tagged "prefix-sum"](https://leetcode.com/tag/prefix-sum/)
|
||
|
|
||
2 years ago
|
### Index as a hash key
|
||
3 years ago
|
|
||
|
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. Examples: [First Missing Positive](https://leetcode.com/problems/first-missing-positive/), [Daily Temperatures](https://leetcode.com/problems/daily-temperatures/)
|
||
|
|
||
|
### Traversing the array more than once
|
||
|
|
||
|
This might be obvious, but traversing the array twice/thrice (as long as fewer than n times) is still O(n). Sometimes traversing the array more than once can help you solve the problem while keeping the time complexity to O(n).
|
||
5 years ago
|
|
||
3 years ago
|
## Essential questions
|
||
|
|
||
|
_These are essential questions to practice if you're studying for this topic._
|
||
5 years ago
|
|
||
|
- [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/)
|
||
|
- [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/)
|
||
|
- [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/)
|
||
3 years ago
|
|
||
|
## Recommended practice questions
|
||
|
|
||
|
_These are recommended questions to practice after you have studied for the topic and have practiced the essential questions._
|
||
|
|
||
|
- [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/)
|
||
5 years ago
|
- [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/)
|
||
|
- [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/)
|
||
3 years ago
|
- [Sliding Window Maximum](https://leetcode.com/problems/sliding-window-maximum/)
|
||
3 years ago
|
|
||
|
## Recommended courses
|
||
|
|
||
|
import AlgorithmCourses from '../\_courses/AlgorithmCourses.md'
|
||
|
|
||
|
<AlgorithmCourses />
|