Implement an immutable 2D point class with translation, distance and value equality
Company: Agoda
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Other
Design an immutable `ImmutablePoint` class representing a point in 2D space. Once an instance is constructed, its `x` and `y` coordinates cannot be modified. In Python, any attempt to assign or delete an attribute on a constructed point should raise `AttributeError`.
Any operation that would conceptually mutate a point (such as translating it) must instead return a new `ImmutablePoint` instance, leaving the original untouched.
Because instances are immutable, they should be comparable by value (two points are equal if both coordinates match) and produce a stable string representation of the form `(x, y)`. Equal points must also hash equally, so points can be used as set members and dictionary keys.
Implement the following:
- `get_x() -> int` returns the x-coordinate of this point.
- `get_y() -> int` returns the y-coordinate of this point.
- `translate(dx: int, dy: int) -> ImmutablePoint` returns a new point shifted by `(dx, dy)`. The original instance is not modified.
- `distance_to(x: int, y: int) -> float` returns the Euclidean distance from this point to the point `(x, y)`.
- `to_string() -> str` returns the canonical string `"(x, y)"`, for example `"(3, -4)"`.
- Value equality and a matching hash (`__eq__` and `__hash__` in Python).
Example 1:
```text
point = ImmutablePoint(3, 4)
point.get_x() -> 3
point.get_y() -> 4
point.to_string() -> "(3, 4)"
point.translate(5, 6).to_string() -> "(8, 10)"
point.get_x() -> 3
point.to_string() -> "(3, 4)"
```
Translating builds a new point at (8, 10). The original still reports x = 3 and prints as `(3, 4)`.
Example 2:
```text
point = ImmutablePoint(0, 0)
point.distance_to(3, 4) -> 5.0
point.distance_to(0, 0) -> 0.0
point.translate(-1, -1).to_string() -> "(-1, -1)"
point.distance_to(0, 0) -> 0.0
point.to_string() -> "(0, 0)"
```
The distance to (3, 4) is the hypotenuse of a 3-4-5 right triangle. After the translation the original is still at the origin, so its distance to (0, 0) is 0.0.
Example 3:
```text
a = ImmutablePoint(1, 2)
b = ImmutablePoint(0, 0).translate(1, 2)
a == b -> True
len({a, b}) -> 1
a.x = 9 -> raises AttributeError
```
The two points are built differently but have the same coordinates, so they are equal and count as one set member. The assignment fails because the instance cannot change after construction.
```hint Where writes happen
Python has no `final` fields. Look at which special method runs on every attribute assignment, and work out how the constructor can still set the two coordinates exactly once.
```
### Constraints & Assumptions
- `-10^9 <= x, y <= 10^9` for the coordinates passed to the constructor.
- `-10^9 <= dx, dy <= 10^9`.
- At most 10,000 operations are performed.
- Coordinates fit in a 64-bit signed integer at all times.
### Clarifying Questions to Ask
- Should `distance_to` also accept another `ImmutablePoint`, or only raw coordinates?
- Must immutability be enforced at runtime, or is a read-only interface enough?
- Should the class be closed to subclassing, so a subclass cannot add mutable state?
- Are coordinates always integers, or can they be floats, which would change the string format and equality?
### What a Strong Answer Covers
- How the coordinates get set exactly once, how later writes and deletes are blocked in the chosen language, and how far that protection goes.
- How `translate` produces a new instance while the receiver stays unchanged.
- Value equality together with a hash that agrees with it, and how comparison with a non-point behaves.
- Integer overflow in coordinate sums and in the distance computation, and the exact string format.
### Follow-up Questions
- How would you keep the class immutable if it also held a mutable field, such as a list of labels?
- Why must `__hash__` be defined alongside `__eq__`, and what goes wrong when a mutable object is used as a dictionary key?
- How do Python frozen dataclasses or Java records compare with writing the class by hand?
- Can an `ImmutablePoint` be shared between threads without locks, and why?
Overview: Implement an immutable 2D point class whose coordinates cannot change after construction, where translation returns a new point and equal points compare and hash the same. It tests immutability techniques, value semantics and careful handling of large coordinates.