DSABeginner6 min readPart 1 of 16

Arrays and Strings

Arrays and strings are the most basic data structures in coding interviews. Nearly every problem touches them in some way, so knowing their operations and what each one costs is non-negotiable.

Can you explain arrays and strings out loud?

I'm Alex. Read this, then I'll interview you on real problems and push on the parts you skip.

ArraysCore idea

An array is one continuous block of memory holding elements of the same type. Because every slot is the same size, the computer can jump straight to any index: that is why access is O(1). Inserting or deleting in the middle is O(n), because every element after it has to shift.

nums739158012345nums[3]Access by index: one jump, O(1).every element after it shifts right73491580123456Insert 4 at index 2: O(n), because the tail has to move.
DiagramWhy reading is cheap and inserting in the middle is not.

Time complexity

OperationTimeWhy
Access by indexO(1)Jump straight to the slot
Search (unsorted)O(n)Look at every element
Search (sorted)O(log n)Binary search halves the range
Insert at endO(1) amortizedOccasional resize, usually free
Insert at beginningO(n)Everything shifts right
Delete at indexO(n)Everything after it shifts left

A common pattern: Two SumPattern

The classic array question. Instead of checking every pair (O(n²)), remember each number you have seen in a hash map, and for every new number ask: have I already seen the value that completes the pair?

Two Sum: O(n) with a hash mapTypeScript
function twoSum(nums: number[], target: number): number[] {
  const map = new Map<number, number>();
  for (let i = 0; i < nums.length; i++) {
    const complement = target - nums[i];
    if (map.has(complement)) return [map.get(complement)!, i];
    map.set(nums[i], i);
  }
  return [];
}
TimeO(n)SpaceO(n)

StringsCore idea

A string is an array of characters. In most languages strings are immutable: you cannot change one in place, so every modification creates a new string and costs O(n). Python strings are immutable; in Java, use StringBuilder to build a string efficiently.

String techniques

Character frequency

Count letters with an array of size 26 (lowercase only) or a hash map.

Reversal

Two pointers from both ends, swapping until they meet. O(n) time, O(1) space.

Anagram check

Sort both strings and compare, or compare their character counts.

Palindrome check

Two pointers from both ends, comparing characters as they move inward.

"racecar"racecar0123456leftrightCompare the ends, then step both pointers inward.
DiagramA palindrome check after one step.
Check if a string is a palindromeTypeScript
function isPalindrome(s: string): boolean {
  let left = 0, right = s.length - 1;
  while (left < right) {
    if (s[left] !== s[right]) return false;
    left++;
    right--;
  }
  return true;
}
TimeO(n)SpaceO(1)

In-place array operationsPattern

Interviewers often ask for O(1) extra space. The trick is to use the array itself as your workspace: one pointer reads, another writes over values you no longer need.

Remove duplicates from a sorted array: O(1) spaceTypeScript
function removeDuplicates(nums: number[]): number {
  let write = 1;
  for (let read = 1; read < nums.length; read++) {
    if (nums[read] !== nums[read - 1]) {
      nums[write] = nums[read];
      write++;
    }
  }
  return write;
}
TimeO(n)SpaceO(1)

Common mistakesPitfalls

Off-by-one errorsClarify whether the array is 0-indexed and whether a range is inclusive or exclusive.
Changing an array while looping over itUse separate read/write pointers, or collect the changes first.
Ignoring edge casesEmpty arrays, a single element, all elements identical.
Using extra space when O(1) is requiredThink about in-place techniques before reaching for a hash map.
Not asking about constraintsSorted or not, duplicates or not, and the input size all change the best approach.

In the interviewIn the interview

Before writing any array code, ask the questions that decide the approach:

Each answer points to a different pattern: sorted suggests two pointers or binary search, unsorted with lookups suggests a hash map, and “in place” suggests read/write pointers.

Frequently asked questionsFAQ

Are strings and arrays the same in coding interviews?

Strings are arrays of characters, but in most languages they are immutable, so concatenation costs O(n) and needs a builder like StringBuilder. The key interview distinction is mutation: arrays support in-place edits while strings require rebuilding, which changes the optimal approach to the problem.

What is the time complexity of array insertion and deletion?

Inserting or deleting at the end of an array is O(1) amortized, while the same at the beginning or middle is O(n) because elements must shift. Random access by index is O(1), searching an unsorted array is O(n), and a sorted array supports O(log n) binary search.

How do you check whether two strings are anagrams?

Use a character frequency count: increment counts for one string and decrement for the other, then confirm every count is zero. That is O(n) time and O(1) space for lowercase letters using a 26-size array, versus O(n log n) for sorting both strings and comparing.

Now explain arrays and strings to me out loud

I'll give you interview problems and ask follow-ups, like a real interviewer.

Practice with Alex

Keep readingKeep reading

Ready to practice?

Reading is step one. Saying it out loud is step two.

Start a mock interview with Alex, get pushed on the parts you skip, and see your hiring signal right after.