2D Vector Class

An immutable value type with operators

Design an immutable 2D vector with natural operator syntax, dot and cross products, rotation, and equality and hashing that agree with each other.

Overview

A 2D vector is a pair of numbers, x and y, that stands for a position, a velocity or a direction on a plane. Games, physics simulations and drawing code create millions of them. The class looks trivial, which is why it is a good interview problem: it tests whether you know how a value type should behave, how Python's operators are wired, and how floating-point numbers break naive equality.

The two decisions that matter are immutability and equality. Immutability means every operation returns a new vector and nothing can change an existing one; that makes vectors safe to share and safe to use as dict keys. Equality must agree with hashing: two vectors that compare equal must hash the same, so the tolerant comparison many people want in eq has to live somewhere else.

A strong answer says 'value type, immutable, exact equality plus a separate tolerant compare' in the first minute, then writes the dunder methods with NotImplemented for unsupported operands.

Scoping the problem

To settle the scope, ask six questions. Each one comes with why it matters and the answer you can usually assume.

  • Are vectors mutable? This decides whether they can be dict keys and be shared safely. Usually they are immutable.
  • Which operators: plus, minus, scalar times, divide, negate, abs? Each is one dunder method. Usually all of these, and no vector times vector with *.
  • Do we need vector times scalar in both orders? 2 * v needs rmul. Usually yes.
  • Exact or tolerant equality? A tolerant eq breaks hashing, so use an exact eq and a separate is_close.
  • Radians or degrees? Every angle method must agree. Use radians, like math.
  • Integers, floats, or both? Mixed input is normal. Accept both and store them as given.

Functional requirements

  • Add and subtract vectors; multiply and divide by a scalar in either order; negate
  • Length via abs(), normalization to a unit vector, distance between two points
  • Dot product, 2D cross product (a scalar), angle from the x axis and angle between two vectors
  • Rotation by an angle in radians, counterclockwise
  • Factories: zero(), unit_x(), unit_y(), from_angle(radians, length)
  • Exact equality and hashing, plus is_close() with tolerances for geometry tests

Non-functional requirements

  • Immutable: no method changes a vector; assignment to x or y raises
  • Usable as dict keys and set members: equal vectors hash equal
  • Unsupported operands return NotImplemented so Python raises a clear TypeError
  • The zero vector never produces NaN: normalized() and angle_to() raise ValueError instead
  • Small memory footprint: slots, two floats per instance

Interview questions

  • Why not compare with a tolerance in eq? It would make the rotation test pass with ==.
  • How would you add a 3D vector without duplicating everything?
  • A physics loop creates a new vector for every operation. Is that a problem?
  • Why do dot and cross have names while add and multiply use operators?
  • What should the zero vector do in normalize and angle?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio