Design a Pokemon battle and trade app with pluggable species classes
Company: Garmin
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a Pokemon trade application that simulates battles and trades between trainers. The system must model a base `Pokemon` type with concrete species (for example `Pikachu` and `Squirtle`), where each species has its own behavior for attacking and defending, while sharing common state such as life points.
Each `Trainer` owns a roster of Pokemon keyed by nickname and can trade a specific Pokemon with another trainer by name. The design must follow the Open/Closed Principle, so new species can be plugged in without modifying existing classes.
To keep results deterministic for testing, the app is created with a seed (`PokemonTradeApp(seed)`) and every probabilistic behavior draws from one seeded pseudo-random generator shared by the whole app. A test may pass its own generator instead (`PokemonTradeApp(rng=...)`, any object with a `random()` method), as Example 2 does. A coin flip succeeds when `rng.random() < 0.5`. The species rules are:
- Pikachu's attack deals `20` damage, then flips a coin. On success the defender is paralyzed, which makes it skip its next attack.
- Squirtle's attack deals `15` damage.
- Squirtle's defense flips a coin on every incoming hit. On success the damage is halved, rounded down.
- A species with no special defense (Pikachu here) takes incoming damage unchanged.
Damage left after any defensive reduction is subtracted from the defender's life points, and life points are clamped at `0`. When Pikachu attacks a Squirtle, the defense coin is flipped first and the paralysis coin second. A paralyzed Pokemon's next attack deals no damage, draws nothing from the generator, and clears the paralysis.
Implement the following methods:
- `add_trainer(trainer_name: str)`: Register a new trainer with an empty roster.
- `add_pokemon(trainer_name: str, species: str, nickname: str)`: Create a Pokemon of the given species (`'Pikachu'` or `'Squirtle'`) with 100 life points and assign it to the trainer under the given nickname.
- `attack(attacker_trainer: str, attacker_nickname: str, defender_trainer: str, defender_nickname: str) -> int`: Have the attacker Pokemon attack the defender Pokemon. Returns the defender's remaining life points after the attack. If the attacker is paralyzed, the attack is skipped (no damage) and paralysis clears.
- `trade(trainer_a: str, nickname_a: str, trainer_b: str, nickname_b: str) -> bool`: Swap the Pokemon registered under `nickname_a` in `trainer_a` with the Pokemon registered under `nickname_b` in `trainer_b`. Each Pokemon keeps its nickname, life points and paralysis status. Returns `True` on success, `False` if either entry is missing.
- `get_life(trainer_name: str, nickname: str) -> int`: Return the current life points of the named Pokemon owned by the given trainer.
- `get_roster(trainer_name: str) -> List[str]`: Return the sorted list of nicknames owned by the given trainer.
Example 1. No output here depends on a random draw, so any seed gives the same results:
```text
app = PokemonTradeApp(seed=7)
app.add_trainer("ash")
app.add_trainer("misty")
app.add_pokemon("ash", "Pikachu", "sparky")
app.add_pokemon("misty", "Squirtle", "bubbles")
app.add_pokemon("misty", "Pikachu", "zap")
app.attack("misty", "bubbles", "ash", "sparky") # -> 85
app.attack("ash", "sparky", "misty", "zap") # -> 80
app.trade("ash", "sparky", "misty", "bubbles") # -> True
app.get_roster("ash") # -> ["bubbles"]
app.get_roster("misty") # -> ["sparky", "zap"]
app.get_life("misty", "sparky") # -> 85
app.trade("ash", "sparky", "misty", "zap") # -> False
```
Squirtle's 15 damage reaches Pikachu unchanged, and Pikachu hitting another Pikachu always deals the full 20 (the paralysis coin only affects zap's next attack). After the trade, sparky belongs to misty with its 85 life points, and ash no longer owns a Pokemon called sparky, so the last trade fails.
Example 2. The app is built with `ScriptedRandom`, a test stub whose `random()` returns `0.84`, `0.76`, `0.42` and `0.26`, in that order:
```text
app = PokemonTradeApp(rng=ScriptedRandom([0.84, 0.76, 0.42, 0.26]))
app.add_trainer("ash")
app.add_trainer("misty")
app.add_pokemon("ash", "Pikachu", "sparky")
app.add_pokemon("misty", "Squirtle", "bubbles")
app.attack("ash", "sparky", "misty", "bubbles") # -> 80 (defense 0.84: full 20; paralysis 0.76: no)
app.attack("ash", "sparky", "misty", "bubbles") # -> 70 (defense 0.42: 20 // 2 = 10; paralysis 0.26: yes)
app.attack("misty", "bubbles", "ash", "sparky") # -> 100 (bubbles is paralyzed: skipped, no draw)
app.attack("misty", "bubbles", "ash", "sparky") # -> 85 (paralysis cleared, 15 damage)
```
Each Pikachu attack on Squirtle uses two draws, defense first. The skipped attack uses none, so the fourth call deals Squirtle's normal 15 damage.
```hint Where species behavior lives
Separate the steps every attack shares from the steps that differ by species. Put the shared steps in one place and give subclasses small methods to override.
```
```hint Adding a third species
Look at how `add_pokemon` turns the string `'Pikachu'` into an object. If that step holds a fixed list of species, adding one means editing a class that already exists.
```
### Constraints & Assumptions
- `1 <= number of trainers <= 10`
- `1 <= number of Pokemon per trainer <= 20`
- Supported species: `Pikachu`, `Squirtle`.
- All Pokemon start with `100` life points.
- Random outcomes come from one generator, seeded at construction or passed in by a test; a coin flip is `True` when `rng.random() < 0.5`.
### Clarifying Questions to Ask
- If a trade would leave a trainer with two Pokemon under the same nickname, should it fail, rename one, or overwrite?
- Can a Pokemon with 0 life points still attack or be traded?
- What should `attack`, `get_life` and `get_roster` do when a trainer or nickname does not exist?
- May a Pokemon attack another Pokemon of the same trainer, or itself?
### What a Strong Answer Covers
- A base class that holds the shared state and the reduce-then-clamp damage step, with species overriding only their attack and defense behavior.
- A way to create a species from its name that lets a new species be added without editing existing classes.
- One seeded generator shared by all Pokemon, with a defined order of draws so tests are reproducible.
- Paralysis handling: which Pokemon gets the flag, how a skipped attack behaves, and when the flag clears.
- A trade that validates both sides before changing anything and moves the Pokemon objects, with their life and status, between rosters.
### Follow-up Questions
- How would you add a species whose attack heals itself, or hits every Pokemon on the defender's roster, without changing the base class?
- How would you test the random behavior without depending on what a particular seed produces?
- If trainers and Pokemon lived in a database, how would you keep two concurrent trades from moving the same Pokemon twice?
- How would you add type effectiveness (Water strong against Fire, for example) without a growing chain of `if` statements?
Overview: Design an object-oriented Pokemon battle and trade app where species plug in without changing existing classes, every random outcome comes from one seeded generator, and trainers swap Pokemon by nickname. It tests the Open/Closed Principle, class hierarchy design, reproducible randomness and safe state changes during a trade.