Array and String Interview Questions

How to approach array and string questions

Array and string questions check the basics: clean loops, correct indexes, and careful handling of edge cases like an empty input or a single item. They're also where interviewers push you past the first working answer toward a faster one.

A handful of techniques cover most of them: two pointers that walk toward each other (or one fast and one slow), a single pass that tracks the best answer so far, sorting the input first, and a hash map that remembers what you've already seen. Many questions also ask you to work in place, using only O(1) extra space, by swapping items within the array instead of building a new one.

Strings add a wrinkle. In Java, Python and JavaScript, strings are immutable, so building a string by concatenating inside a loop can take O(n²) time. Collect the pieces in a list or a string builder and join them at the end.

Interview Cake isn't just another question database. Each question below walks you through the problem step by step, with hints and explanations as you need them, just like a real interviewer.

Concepts to review

  • Arrays: How arrays are laid out in memory, and why looking up an item by index is O(1).
  • Dynamic arrays: Arrays that grow as you append, like Python lists and Java ArrayLists.
  • Array slicing: Handy, but it copies: a slice of length k costs O(k) time and space.
  • In-place algorithms: Changing the input directly with O(1) extra space, which many array questions ask for.
  • Hash tables: The go-to tool for counting characters and finding pairs in one pass.
  • Binary search: Finding an item in a sorted array in O(log n) time.

Quick reference: Big O cheat sheet · Data structures cheat sheet

Array and string interview FAQ

What are the most common array interview questions?

Classic ones include two sum, the maximum subarray sum, merging overlapping intervals, rotating an array, removing duplicates from a sorted array, the product of every other number, and merging two sorted arrays.

What is the two-pointer technique?

It means walking an array with two indexes at once: usually one at each end moving toward the middle, or a fast one and a slow one moving the same way. It solves problems like reversing an array in place or finding a pair with a given sum in a sorted array in O(n) time and O(1) extra space.

What does "in place" mean in an array question?

It means changing the input array directly, using only O(1) extra space, instead of building a new array. Reversing an array by swapping items from both ends toward the middle is a typical in-place algorithm.

How do you reverse a string in place?

Swap the first and last characters, move both indexes one step toward the middle, and repeat until they meet. In languages where strings are immutable, like Java and Python, convert the string to an array of characters first and build a new string from it at the end.

Are strings just arrays of characters?

In C, a string is an array of characters ending in a null character. In Java, Python and JavaScript, a string acts like a read-only array: you can read any character by index in O(1) time, but you can't change it in place, so every change creates a new string.
. . .