Skip to content
Interview Prep
All Problems

Monster Battle

MediumOpenAI Interview
SimulationTurn-BasedOOP

Two teams of monsters fight based on a set of rules until one team is fully defeated. On a high level this can be represented as a function taking two teams as parameters and returning the outcome containing a battle log. At a high level, the API looks like:

outcome = battle(team1, team2)

A team has a name and one or more monsters. Each monster has:

Name
Number of life points
Attack strength

The monsters fight one-on-one and attack each other in turns:

The team that is the first argument to battle goes first
The monsters fight in the same order they are listed in their team
On each turn a monster attacks by subtracting its "attack" from the opponent's life points
Then it's time for the opponent to attack and do the same, until one has life points reduced to 0
When a monster from a team is defeated, the next one from the same team takes its place and continues

The outcome contains:

Name of the winning team
Log of messages on the events, e.g. "Wolf attacks Bear for 5 damage", "Bear is defeated", ...

Please design the types for representing monsters, teams, and outcomes, and implement the battle function.

Part 2 — Type Weaknesses

Each monster has a type attribute (e.g. "fire", "water") and a weakness attribute representing a specific type. During an attack:

If the attacker's type matches the defender's weakness, the damage is doubled
If the defender's type matches the attacker's weakness, the damage is halved (rounded down)

Update the event log to include information about type advantages or disadvantages when they affect the damage calculation.

Part 3 — Smart Attacker Selection

Change the rules so the attacking team acts smarter. Instead of always using the first available monster, they should pick the monster that deals the most damage to the current defender.

Class Skeleton

1from dataclasses import dataclass
2
3# Design your Monster, Team, and Outcome types here
4
5
6def battle(team1, team2):
7    """
8    Simulate a battle between two teams of monsters.
9    Returns an Outcome with the winning team name
10    and a log of battle events.
11    """
12    pass

Examples

Example — Part 1

1# Team Blue
2# Dog:  3 life points, 2 attack
3# Wolf: 4 life points, 1 attack
4
5# Team Red
6# Cat:   3 life points, 3 attack
7# Tiger: 4 life points, 5 attack
8
9outcome = battle(team_blue, team_red)
10
11# Battle log:
12# Team Blue turn
13# Dog attacks Cat for 2 damage
14# Team Red turn
15# Cat attacks Dog for 3 damage
16# Dog is defeated
17# Team Blue turn
18# Wolf attacks Cat for 1 damage
19# Cat is defeated
20# Team Blue turn
21# Wolf attacks Tiger for 1 damage
22# Team Red turn
23# Tiger attacks Wolf for 5 damage
24# Wolf is defeated
25# Team Red wins!

Mock Interview Prompt

Copy this prompt into ChatGPT to practice with an AI interviewer.

Act as a senior SWE interviewing me for an entry-level to mid-level engineer position.

I have not done interviews in a long time. Do this step by step:
1. Present the problem below, then ask if I have any clarifying questions before I start. If I say no or try to skip ahead, gently nudge me — say something like "Are you sure? It might be worth thinking about edge cases around HP or the battle loop before diving in." Do NOT give specific example questions — just hint at the area.
2. Answer my clarifying questions if I have any, then ask me to walk you through my approach before coding.
3. Once I start coding, give hints only if I appear stuck. Never directly give me the answer — always end with a question that guides me.
4. When I make a suboptimal choice or get something wrong, briefly explain why the alternative is better so I learn the reasoning, then ask me how I'd adjust.
5. Give positive reinforcement when I get something right or show good instincts.

Here is the problem:

Two teams of monsters fight based on a set of rules until one team is fully defeated. On a high level this can be represented as a function taking two teams as parameters and returning the outcome containing a battle log:

outcome = battle(team1, team2)

A team has a name and one or more monsters. Each monster has a name, a number of life points, and an attack strength.

The monsters fight one-on-one and attack each other in turns:
- The team that is the first argument to battle goes first
- The monsters fight in the same order they are listed in their team
- On each turn a monster attacks by subtracting its "attack" from the opponent's life points
- Then it's time for the opponent to attack and do the same, until one has life points reduced to 0
- When a monster from a team is defeated, the next one from the same team takes its place and continues

The outcome contains the name of the winning team and a log of messages on the events, e.g. "Wolf attacks Bear for 5 damage", "Bear is defeated".

Design the types for representing monsters, teams, and outcomes, and implement the battle function.

Example:

Team Blue — Dog: 3 life points, 2 attack; Wolf: 4 life points, 1 attack
Team Red — Cat: 3 life points, 3 attack; Tiger: 4 life points, 5 attack

Battle log:
Team Blue turn
Dog attacks Cat for 2 damage
Team Red turn
Cat attacks Dog for 3 damage
Dog is defeated
Team Blue turn
Wolf attacks Cat for 1 damage
Cat is defeated
Team Blue turn
Wolf attacks Tiger for 1 damage
Team Red turn
Tiger attacks Wolf for 5 damage
Wolf is defeated
Team Red wins!

Once the candidate finishes Part 1, move on to the follow-ups in order:

Part 2 — Type Weaknesses:
Each monster has a type attribute (e.g. "fire", "water") and a weakness attribute representing a specific type. During an attack, if the attacker's type matches the defender's weakness, the damage is doubled. If the defender's type matches the attacker's weakness, the damage is halved (rounded down). Update the event log to include information about type advantages or disadvantages when they affect the damage calculation.

Part 3 — Smart Attacker Selection:
Change the rules so the attacking team acts smarter. Instead of always using the first available monster, they should pick the monster that deals the most damage to the current defender.
solution.py