AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started

Topic

Arrays coding problems

The array is the first data structure every other one is built on: a block of values addressed by index in constant time.

New to arrays? Read the Arrays tutorial first.

1199 problems · 320 easy · 633 medium · 246 hard · judged by hidden tests in 13 languages

1199problems

320 easy

633 medium

246 hard

Sign in to tick off what you solve.

Study plan: 14 problems · about 8 h 10 min

Start the study plan

Next up: Running Sum of 1d Array

How it works

Arrays, step by step

Best time to buy and sell stock, with one pass and a running minimum

prices = [7, 1, 5, 3, 6, 4]

price701152336445cheapest so far: —sell today: —best profit: 0

Selling on day i earns prices[i] minus the cheapest price before it. So one pass only has to remember two numbers: the cheapest price so far and the best profit so far.

0:01 / 0:26Step 1 of 8
Transcript · 8 steps

Study plan

Learn arrays in 8 days

14 of the 1199 Arrays problems (4 easy, 7 medium and 3 hard) over 8 days, about 8 h 10 min in all — the pattern first, then easiest to hardest. After that, the other 1185 in the full list below are practice at your own pace. Then move on to Strings.

  1. Day 1

    about 1 h 15 min

    Learn the pattern: read the essentials and step through the walkthrough above, then solve these 3.

  2. Day 2

    about 55 min

    Medium problems: the same pattern with one twist each. Name the twist before you code.

  3. Day 3

    about 1 h 10 min

    More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

  4. Day 4

    about 1 h 10 min

    More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

  5. Day 5

    about 1 h 10 min

    More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

  6. Day 6

    about 50 min

    Hard problems: the pattern combined with a second idea. Give each a full attempt before reading the editorial.

  7. Day 7

    about 50 min

    Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

  8. Day 8

    about 50 min

    Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

    Next topic: Strings

Arrays Coding Problems: 1199 Questions with Solutions

1199 arrays coding problems — 320 easy · 633 medium · 246 hard — with solutions in 13 languages. Plus a step-by-step walkthrough and a 8-day plan.

  • Problems: 1199
  • By difficulty: 320 easy · 633 medium · 246 hard
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
  • Cost: Free on every plan; sign in to run and submit

The array is the first data structure every other one is built on: a block of values addressed by index in constant time. Array problems practise the moves that recur everywhere else — scanning once, keeping a running best, working from both ends, sorting first to make a hard question easy, and reasoning about indices without going off either end. Most of the catalogue touches an array somewhere; the problems here are the ones where the array itself is the point.

New to arrays? Read the Arrays tutorial first.

How arrays works, step by step

price701152336445buysellcheapest so far: 1 (day 1)trade: 6 − 1 = 5best profit: 5 (buy day 1, sell day 4)
Best time to buy and sell stock, with one pass and a running minimum. Example: prices = [7, 1, 5, 3, 6, 4]
  1. Selling on day i earns prices[i] minus the cheapest price before it. So one pass only has to remember two numbers: the cheapest price so far and the best profit so far.
  2. Day 0 costs 7. Nothing came before it, so 7 is the cheapest price so far and there is nothing to sell yet.
  3. Day 1 costs 1, below the old cheapest 7, so day 1 becomes the day to buy. Selling on the day you buy earns nothing, and the best profit stays 0.
  4. Day 2 costs 5. Buying at the cheapest earlier price (1, day 1) and selling today earns 5 − 1 = 4, so best = 4. Earlier days other than the cheapest can be forgotten: none of them is a better day to buy.
  5. Day 3 costs 3: selling today would earn 2, less than the best 4, and 3 is not below 1, so neither number changes.
  6. Day 4 costs 6. Buying at the cheapest earlier price (1, day 1) and selling today earns 6 − 1 = 5, so best = 5.
  7. Day 5 costs 4: selling today would earn 3, less than the best 5, and 4 is not below 1, so neither number changes.
  8. The best trade is to buy on day 1 at 1 and sell on day 4 at 6, a profit of 5. Each day was looked at once with two numbers kept, so it is O(n) time and O(1) space.

Arrays study plan

14 of the 1199 Arrays problems (4 easy, 7 medium and 3 hard) over 8 days, about 8 h 10 min in all — the pattern first, then easiest to hardest. After that, the other 1185 in the full list below are practice at your own pace. Then move on to Strings.

Day 1

Learn the pattern: read the essentials and step through the walkthrough above, then solve these 3.

Day 2

Medium problems: the same pattern with one twist each. Name the twist before you code.

Day 3

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 4

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 5

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 6

Hard problems: the pattern combined with a second idea. Give each a full attempt before reading the editorial.

Day 7

Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

Day 8

Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

Next topic: Strings

Arrays: the essentials

When to reach for it

The input is a list of numbers and the question is about positions, order or aggregates: the best pair of days, an element's neighbours, a value's frequency, a rearrangement that must happen in place. With n up to 10⁵, comparing every pair is about 5 × 10⁹ steps, so the answer must come from one or two passes. "In place" or "O(1) extra space" means the array itself must hold the working state.

The pattern

Most array solutions are a single pass that carries a little state: the smallest value so far, a write index, a running total. Ask what you need to know about everything before index i to answer for i, keep exactly that, and update it in constant time. When the answer at i depends on both sides, a pass from the left and a pass from the right usually suffice — the idea behind Prefix Sum.

def max_profit(prices):
    best, low = 0, float("inf")
    for p in prices:
        low = min(low, p)           # cheapest day so far
        best = max(best, p - low)   # sell today
    return best

Cost

One pass is O(n) time. The carried state is O(1) space, or O(n) when you keep a prefix or suffix array. Sorting first costs O(n log n) and usually reorders the caller's array.

Common mistakes

  • Reading nums[i + 1] or nums[i - 1] without guarding the ends.
  • Removing elements while looping by index, which skips the element that slides into the gap.
  • Starting a running maximum at 0 when every value can be negative.
  • Returning a new array when the problem wants the input modified and a length returned.

Start with

All arrays problems

Easy (320)

Medium (633)

Hard (246)

Companies that ask arrays problems

Next topic: Strings