Simulate Turn-Based Monster Battles
Company: OpenAI
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Simulate a turn-based battle between two teams of monsters.
Base problem:
- Team A and Team B are ordered lists of monsters.
- Each monster has at least `hp` and `atk`.
- Team A attacks first.
- On each action, the active attacker deals damage equal to its `atk` to the opposing active monster.
- If a monster's HP drops to `0` or below, it is removed immediately and the next monster on that team becomes active.
- Turns alternate after every action, even if an attack causes a knockout.
- The battle ends when one team has no monsters left.
Return:
- Winner: `A`, `B`, or `Draw` if your rules allow it
- Remaining HP of the winner's current active monster
- Total number of attacks
Common follow-ups:
1. Add monster types and a type-effectiveness table. Damage becomes `floor(atk * multiplier)` with minimum damage `1`.
2. Give each monster multiple moves. On each turn, the attacker chooses the move that yields the highest current damage against the defender; break ties by smallest move index.
3. Optimize repeated battles when a monster may have a very large move list, so scanning all moves every turn is too slow.
This question tests simulation correctness, turn semantics, state updates after knockouts, tie-breaking, and incremental optimization.
Quick Answer: This question evaluates the ability to implement a turn-based battle simulation with correct state management, turn semantics, knockout handling, damage computation, tie-breaking, and performance-aware move selection and type-effectiveness logic.