TRT
Sign in
easyFoundationscoding

How would you delete many elements from an array efficiently?

Explain why repeated single deletes are quadratic, then give the one-pass compaction, the swap-with-last trick and when to change data structure.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team2 min read
Learn it firstInserting and deleting in the middleAn array has no gaps, so inserting or deleting anywhere but the end shifts everything after that point. Knowing that cost, and the tricks that avoid it, decides how you use arrays.

A strong answer

A strong candidate frames How would you delete many elements from an array efficiently? around the exact memory layout, compiler semantics, and hardware execution model covered in Inserting and deleting in the middle. Explain why repeated single deletes are quadratic, then give the one-pass compaction, the swap-with-last trick and when to change data structure.

An array's power comes from its formula: element i is at base + i × size. That formula only holds if the elements sit side by side with no gaps. So when you insert or delete anywhere except the end, the array has to restore that property by moving elements.

To insert at index i, every element from i to the end moves one place right, opening a slot. To delete at index i, every element after it moves one place left, closing the gap. Either way, the number of moves is the number of elements after the position: n − i.

In a production interview, always connect the specification rule to its concrete runtime consequence: state what lives on the stack versus the heap or static storage segment, how the compiler or operating system validates alignments, lifetimes, and error codes, and what happens under adversarial or boundary inputs (such as zero-length buffers, integer overflow, concurrent mutation, or partial I/O). Backing your explanation with a small trace of register/memory state transitions immediately separates senior systems engineers from candidates who only memorized syntax.

What the interviewer is listening for

  • Mastery of core invariant: Trace an insert and a delete in the middle of an array, and count the moves.
  • Mastery of core invariant: Delete in O(1) when order doesn't matter, and many elements in one O(n) pass.
  • Mastery of core invariant: Choose a structure when front or middle edits are frequent.
  • Hardware and ABI awareness: Explaining how sizes, alignments, calling conventions, or kernel boundaries behave on real 64-bit targets rather than relying on implementation-defined assumptions.
  • Defensive engineering: Naming the exact compiler flags (-Wall -Wextra -Werror), static analyzers, or runtime sanitizers (-fsanitize=address,undefined,thread) that verify correctness in CI.

Common mistakes

  • Explaining only surface syntax without tracing the underlying memory addresses, ownership lifetime, or kernel/user-space state transitions.
  • Assuming undefined behavior (such as signed overflow, uninitialized reads, strict-aliasing violations, or dangling pointers) will "just work" because it passed an unoptimized -O0 debug build.
  • Ignoring return codes, partial reads/writes, alignment padding, or cleanup paths when an allocation or syscall fails midway through a function.

This answer walks

Practice follow-ups

  1. 01What changes if the order of the remaining elements doesn't matter?
  2. 02Why is deleting while iterating forward over a Python list a bug, and why does iterating backward avoid it?
  3. 03How do databases and hash tables delete without shifting?
  4. 04When would you switch to a linked list or a deque instead?

Not started

Sign in to save your learning progress.

Sign in to save