Implement a 2D vector class with addition, dot product, magnitude, normalization and scaling

Quick Overview

Implement a two-dimensional vector class that supports addition, subtraction, dot product, magnitude, normalization and scaling, with every result rounded to five decimal places. It tests clean class design, careful floating-point handling and edge cases such as the zero vector.

Implement a 2D vector class with addition, dot product, magnitude, normalization and scaling

Company: Epic Games

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

Design and implement a `Vector2D` class that represents a vector in two-dimensional space with components `x` and `y`. The class should support the common vector operations: addition, subtraction, dot product, magnitude, normalization to a unit vector, reading the current components, and scaling by a scalar value. All floating-point results are rounded to 5 decimal places. Normalizing a zero vector (both components are `0`) returns `[0.0, 0.0]`. The class has the methods below. None of them changes the stored vector: in Example 1, `subtract` still works on `(3, 4)` after the `add` call. | Method | Returns | Behavior | |---|---|---| | `Vector2D(x: float, y: float)` | constructor | Creates the vector `(x, y)`. | | `add(x: float, y: float)` | `List[float]` | Adds the vector `(x, y)` to this vector and returns the result as `[rx, ry]`. | | `subtract(x: float, y: float)` | `List[float]` | Subtracts the vector `(x, y)` from this vector and returns the result as `[rx, ry]`. | | `dot(x: float, y: float)` | `float` | Returns the dot product of this vector with the vector `(x, y)`. | | `magnitude()` | `float` | Returns the magnitude (Euclidean length) of this vector. | | `normalize()` | `List[float]` | Returns the unit vector as `[nx, ny]`, or `[0.0, 0.0]` when the magnitude is zero. | | `getComponents()` | `List[float]` | Returns the current components as `[x, y]`. | | `scale(scalar: float)` | `List[float]` | Returns this vector multiplied by `scalar` as `[rx, ry]` without modifying the original vector. | Calls are given as a list of operation names and a matching list of argument lists. The output holds `null` for the constructor and the return value of every other call. Example 1: ```text Input: operations = ["Vector2D", "add", "subtract", "dot", "magnitude", "normalize", "getComponents", "scale"] arguments = [[3, 4], [1, 2], [1, 1], [3, 4], [], [], [], [2]] Output: [null, [4.0, 6.0], [2.0, 3.0], 25.0, 5.0, [0.6, 0.8], [3.0, 4.0], [6.0, 8.0]] ``` The vector is `(3, 4)`. `add(1, 2)` returns `[4.0, 6.0]`, `subtract(1, 1)` returns `[2.0, 3.0]`, `dot(3, 4)` is `3*3 + 4*4 = 25.0`, the magnitude is `5.0`, the unit vector is `[0.6, 0.8]`, the components are `[3.0, 4.0]` and `scale(2)` returns `[6.0, 8.0]`. Example 2: ```text Input: operations = ["Vector2D", "magnitude", "normalize", "dot", "add"] arguments = [[0, 0], [], [], [5, 5], [1, 1]] Output: [null, 0.0, [0.0, 0.0], 0.0, [1.0, 1.0]] ``` For the zero vector the magnitude is `0.0`, `normalize()` returns `[0.0, 0.0]` by the zero-vector rule, `dot(5, 5)` is `0.0` and `add(1, 1)` returns `[1.0, 1.0]`. ```hint Where the rounding goes Decide whether rounding belongs on the stored components or only on the values you return, then check what each choice does to a vector whose components are smaller than 0.00001. ``` ### Constraints & Assumptions - `-10⁴ <= x, y <= 10⁴` - All input values are valid floating-point numbers. - Normalizing a zero vector returns `[0.0, 0.0]`. - All floating-point results are rounded to 5 decimal places. ### Clarifying Questions to Ask - Should `add`, `subtract` and `scale` leave the stored vector unchanged, or should any of them update it? - Does the 5-decimal rounding apply to the stored components or only to the values returned? - Which rule breaks an exact tie when rounding: half up or half to even? - Should a result of `-0.0` be reported as `0.0`? ### What a Strong Answer Covers - Methods that compute new results without modifying the stored components, consistent with the call sequence in Example 1. - Correct formulas for the dot product, magnitude and unit vector, with the zero vector handled before any division. - A deliberate decision about where rounding is applied and how that choice affects later calls. - Floating-point details such as underflow in the magnitude, negative zero and tie-breaking in rounding. - Constant time and space per operation, plus tests that go beyond the given examples, including one where rounding changes the digits. ### Follow-up Questions - How would you change the API so methods take and return `Vector2D` objects, and how would you support `v1 + v2` and `3 * v` in Python? - How would you test two vectors for equality when their components carry floating-point error? - What would you change to use this type in a per-frame update loop over millions of vectors? - How would you generalize the class to N dimensions without writing each method once per component?

Overview: Implement a two-dimensional vector class that supports addition, subtraction, dot product, magnitude, normalization and scaling, with every result rounded to five decimal places. It tests clean class design, careful floating-point handling and edge cases such as the zero vector.

Epic Games logo
Epic Games
Nov 24, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design and implement a Vector2D class that represents a vector in two-dimensional space with components x and y.

The class should support the common vector operations: addition, subtraction, dot product, magnitude, normalization to a unit vector, reading the current components, and scaling by a scalar value.

All floating-point results are rounded to 5 decimal places. Normalizing a zero vector (both components are 0) returns [0.0, 0.0].

The class has the methods below. None of them changes the stored vector: in Example 1, subtract still works on (3, 4) after the add call.

MethodReturnsBehavior
Vector2D(x: float, y: float)constructorCreates the vector (x, y).
add(x: float, y: float)List[float]Adds the vector (x, y) to this vector and returns the result as [rx, ry].
subtract(x: float, y: float)List[float]Subtracts the vector (x, y) from this vector and returns the result as [rx, ry].
dot(x: float, y: float)floatReturns the dot product of this vector with the vector (x, y).
magnitude()floatReturns the magnitude (Euclidean length) of this vector.
normalize()List[float]Returns the unit vector as [nx, ny], or [0.0, 0.0] when the magnitude is zero.
getComponents()List[float]Returns the current components as [x, y].
scale(scalar: float)List[float]Returns this vector multiplied by scalar as [rx, ry] without modifying the original vector.

Calls are given as a list of operation names and a matching list of argument lists. The output holds null for the constructor and the return value of every other call.

Example 1:

Input:
operations = ["Vector2D", "add", "subtract", "dot", "magnitude", "normalize", "getComponents", "scale"]
arguments  = [[3, 4], [1, 2], [1, 1], [3, 4], [], [], [], [2]]
Output: [null, [4.0, 6.0], [2.0, 3.0], 25.0, 5.0, [0.6, 0.8], [3.0, 4.0], [6.0, 8.0]]

The vector is (3, 4). add(1, 2) returns [4.0, 6.0], subtract(1, 1) returns [2.0, 3.0], dot(3, 4) is 3*3 + 4*4 = 25.0, the magnitude is 5.0, the unit vector is [0.6, 0.8], the components are [3.0, 4.0] and scale(2) returns [6.0, 8.0].

Example 2:

Input:
operations = ["Vector2D", "magnitude", "normalize", "dot", "add"]
arguments  = [[0, 0], [], [], [5, 5], [1, 1]]
Output: [null, 0.0, [0.0, 0.0], 0.0, [1.0, 1.0]]

For the zero vector the magnitude is 0.0, normalize() returns [0.0, 0.0] by the zero-vector rule, dot(5, 5) is 0.0 and add(1, 1) returns [1.0, 1.0].

Constraints & Assumptions

  • -10⁴ <= x, y <= 10⁴
  • All input values are valid floating-point numbers.
  • Normalizing a zero vector returns [0.0, 0.0] .
  • All floating-point results are rounded to 5 decimal places.

Clarifying Questions to Ask Guidance

  • Should add , subtract and scale leave the stored vector unchanged, or should any of them update it?
  • Does the 5-decimal rounding apply to the stored components or only to the values returned?
  • Which rule breaks an exact tie when rounding: half up or half to even?
  • Should a result of -0.0 be reported as 0.0 ?

What a Strong Answer Covers Guidance

  • Methods that compute new results without modifying the stored components, consistent with the call sequence in Example 1.
  • Correct formulas for the dot product, magnitude and unit vector, with the zero vector handled before any division.
  • A deliberate decision about where rounding is applied and how that choice affects later calls.
  • Floating-point details such as underflow in the magnitude, negative zero and tie-breaking in rounding.
  • Constant time and space per operation, plus tests that go beyond the given examples, including one where rounding changes the digits.

Follow-up Questions Guidance

  • How would you change the API so methods take and return Vector2D objects, and how would you support v1 + v2 and 3 * v in Python?
  • How would you test two vectors for equality when their components carry floating-point error?
  • What would you change to use this type in a per-frame update loop over millions of vectors?
  • How would you generalize the class to N dimensions without writing each method once per component?