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.
133 lines
8.7 KiB
133 lines
8.7 KiB
---
|
|
id: array
|
|
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
|
|
toc_max_heading_level: 2
|
|
---
|
|
|
|
<head>
|
|
<meta property="og:image" content="https://www.techinterviewhandbook.org/social/algorithms/algorithms/algorithms-array.png" />
|
|
</head>
|
|
|
|
## Introduction
|
|
|
|
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!
|
|
|
|
**Advantages**
|
|
|
|
- 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.
|
|
|
|
**Disadvantages**
|
|
|
|
- Addition and removal of elements into/from the middle an array is slow because they 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.
|
|
|
|
## 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
|
|
|
|
Common terms you see when doing problems involving arrays:
|
|
|
|
- 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.
|
|
|
|
## Time complexity
|
|
|
|
| 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 |
|
|
|
|
## Things to look out for during interviews
|
|
|
|
- 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.
|
|
|
|
## Corner cases
|
|
|
|
- Empty sequence
|
|
- Sequence with 1 or 2 elements
|
|
- Sequence with repeated elements
|
|
- 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/)
|
|
|
|
### Index has a hash key
|
|
|
|
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).
|
|
|
|
## Recommended 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/)
|
|
- [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/)
|
|
- [Sliding Window Maximum](https://leetcode.com/problems/sliding-window-maximum/)
|
|
|
|
## Recommended courses
|
|
|
|
import AlgorithmCourses from '../\_courses/AlgorithmCourses.md'
|
|
|
|
<AlgorithmCourses />
|