Medium Arrays

A basic linear data structure that holds a fixed number of containers to store data.

Section Menu

How do I use this section?

1. LESSON

A Gentle Refresher Into Arrays

Let's take a scenic stroll through the world of arrays and strings and discover the connections between our everyday lives and computer science. We'll explore this fascinating subject through a structured and engaging lesson. Objective In this lesson, we will: **Understand Arrays and Strings:...

2. LESSON

Stringing Together Strings

Arrays With Characters A word with 4 characters ['M', 'O', 'N', 'A'] in a character array can be represented as follows: You see, each character gets a place in each index. Does this seem oddly familiar? There’s a more convenie...

3. LESSON

String Manipulation: Techniques and Best Practices

Why String Manipulation Matters In the ever-evolving world of software development, string manipulation stands as a fundamental and vital skill. Strings are at the heart of human-readable data. Whether it's processing user input, reading files, generating dynamic content for web pages, or simply communicating between systems,...

4. LESSON

The Set Data Structure and Set Theory

A big portion of data structures used in modern programming are directly translated from mathematics. One crucial example is the set data structure and its operations, derived from Set Theory. Understanding this data structure requires at least some prior knowledge of the mathematical perspective of th...

5. LESSON

Using the Two Pointer Technique

The Two Pointer Technique The two pointer technique is a near necessity in any software developer's toolkit, especially when it comes to technical interviews. In this guide, we'll cover the basics so that you know when and how to use this technique. <img src="https://storage.googleapis.com/algodailyrandomassets/curriculum/arrays/two-p...

6. LESSON

A Bird’s Eye View into the Sliding Windows Algorithm Pattern

A Bird’s Eye View into Sliding Windows Objective: In this lesson, we'll cover this concept, and focus on these outcomes: You'll learn what the sliding windows algorithm pattern is. We'll show you how and when to use sliding windows in programming interviews. We'll wal...

7. LESSON

The Binary Search Technique And Implementation

Objective: This article will cover the binary search technique or pattern, one of the most commonly used methods to solve a variety of problems. By the end, you should: Be familiar with the binary search algorithm. See how it's used in interviews. Understand its complexities. Binary Sea...

8. LESSON

A Close Look at Merging Intervals

Objective: In this lesson, we'll cover this concept, and focus on these outcomes: You'll learn what merging intervals means. We'll show you how to solve similar time-based, or interval-based problems in programming interviews. Let's study the Merge Intervals algorithm which is used to...

9. LESSON

Fundamental Sorting Algorithms: Bubble and Insertion

This tutorial is about two fundamental, and basic, sorting algorithms. If you are new to sorting, I'd recommend you come up with your own algorithm for sorting arrays first, and then compare it with these algorithms. These fundamental sorting techniques give you important insights into: How to sort arrays How you can improve an algorit...

10. LESSON

Merge Sort vs. Quick Sort vs. Heap Sort

In this tutorial, we are going to discuss three O (n log n) sorting techniques, their implementations, and how we derive their runtimes. The learning objectives of this tutorial are as follows: You will be able to apply the Divide-and-Conquer approach to different sorting methods. You will be able t...

11. LESSON

Prefix, Infix, Postfix Notation and Prefix Sums

There are a few different ways to write arthimetic expressions in computer science. Infix, Postfix and Prefix notations are three different but equivalent ways of doing this and achieving the same result in the end. In this tutorial, we are going to explain these three, give some examples of their usage, and dig into the `Prefix Sums algorit...

12. LESSON

Cycling Through Cycle Sort

In this lesson, we are going to study the Cycle Sort algorithm, which is one of the less commonly used sorting algorithms, but surprisingly useful in programming interviews. The Theory Th...

13. CHALLENGE

Max of Min Pairs

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...

14. CHALLENGE

Missing Number In Unsorted

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...

15. CHALLENGE

Find Missing Number in Array

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...

16. CHALLENGE

Sum All Primes

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...

17. CHALLENGE

Fast Minimum In Rotated Array

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...

18. CHALLENGE

Remove Duplicates From Array

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...

19. CHALLENGE

Contiguous Subarray Sum

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...

20. CHALLENGE

Treats Distribution

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...

21. CHALLENGE

Least Missing Positive Number

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...

22. CHALLENGE

Product Except Self

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...

23. CHALLENGE

Two Sum

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...

24. CHALLENGE

Is A Subsequence

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....

25. CHALLENGE

Sorted Two Sum

The setup is the same as Two Sum-- you're given an array of numbers, and a "goal" number. Write a method to return an array of the indexes of the two elements in the array that sum up to the goal. If there are no such elements, return an empty array. The caveat here is that the numbers are guaranteed to...

26. CHALLENGE

Stock Buy and Sell Optimization

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...

27. CHALLENGE

How Out of Order

This was submitted by user Hackleman. Determine how "out of order" a list is by writing a function that returns the number of inversions in that list. Two elements of a list L, L[i] and L[j], form an inversion if L[i] j. <img src="https://storage.googleapis.com/algodailyrandomassets/curriculum/me...

28. CHALLENGE

Max Leaps in Jump Game

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...

29. CHALLENGE

Split Set to Equal Subsets

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...

30. CHALLENGE

Count the Planes

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...

31. CHALLENGE

Merge Intervals

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...

32. CHALLENGE

Zeros to the End

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="...

33. CHALLENGE

Next Greater Element in a Circular Array

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...

34. CHALLENGE

K Largest Elements From List

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....

35. CHALLENGE

Classical N-Queen Problem

N-Queen Problem: The Chessboard Conundrum Imagine stepping into an interview room, and the interviewer asks you to solve the N-Queen problem. It's a modern take on the classic 8-Queen Puzzle, where you place queens on a chessboard in such a way that none can attack another. He...

Cheat Sheet

  • Quick summary: a collection that stores elements in order and looks them up by index.
  • Also known as: fixed array, static array.
  • Important facts:
    • Stores elements sequentially, one after another.
    • Each array element has an index. Zero-based indexing is used most often: the first index is 0, the second is 1, and so on.
    • Is created with a fixed size. Increasing or decreasing the size of an array is impossible.
    • Can be one-dimensional (linear) or multi-dimensional.
    • Allocates contiguous memory space for all its elements.
  • Pros:
    • Ensures constant time access by index.
    • Constant time append (insertion at the end of an array).
  • Cons:
    • Fixed size that can't be changed.
    • Search, insertion and deletion are O(n). After insertion or deletion, all subsequent elements are moved one index further.
    • Can be memory intensive when capacity is underused.
  • Notable uses:
    • The String data type that represents text is implemented in programming languages as an array that consists of a sequence of characters plus a terminating character.
  • Time complexity (worst case):
    • Access: O(1)
    • Search: O(n)
    • Insertion: O(n) (append: O(1))
    • Deletion: O(n)
  • See also: