TRT
Sign in
mediumAdvancedcoding

Find all unique triples that sum to zero

Solve 3Sum in O(n²) with sorting and two pointers, and avoid duplicate triples without a set.

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

The Runtime Theory Team2 min read
Learn it firstTwo pointersPro — members onlyOn a sorted array, two indexes moving toward each other can find pairs with a given sum in one pass, because every move rules out a whole row of candidate pairs. The same argument powers three-sum, container problems and trapping rain water.

A strong answer

A strong candidate frames Find all unique triples that sum to zero around the exact memory layout, compiler semantics, and hardware execution model covered in Two pointers. Solve 3Sum in O(n²) with sorting and two pointers, and avoid duplicate triples without a set.

The arrays module introduced two pointers as a way to process an array in one pass with O(1) extra memory: reversing, comparing ends, compacting. This module collects the patterns that turn quadratic searches into linear ones, and converging two pointers is the first. Its power comes from a specific kind of argument: every time a pointer moves, a whole group of candidates is ruled out at once, without being examined.

Take a sorted array and a target, and look for two values that sum to it. There are n(n − 1)/2 pairs; checking them all is O(n²). Instead, start lo at the smallest value and hi at the largest, and look at their sum.

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: Find a pair with a given sum in a sorted array in O(n) with converging pointers.
  • Mastery of core invariant: Explain why each pointer move is safe, in terms of the pairs it rules out.
  • Mastery of core invariant: Extend the pattern to three-sum and to problems where the pointer that moves is the limiting one.
  • 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. 01Why is each two-pointer move safe?
  2. 02How would you generalize to k-sum?
  3. 03Can you do better than O(n²)? What is known about 3Sum's lower bound?
  4. 04How would you solve it if sorting were not allowed?

Not started

Sign in to save your learning progress.

Sign in to save