Level: Senior-Level
Round: Phone Screen · Type: Coding · Difficulty: 5/10 · Duration: 60 min
Topics: Simulation
Location: San Francisco Bay Area
Interview date: 2026-01-14
Question: Simulating a deterministic, turn-based monster battle between two ordered teams of monsters. The system executes the fight step by step, producing a complete, chronological battle log describing every event.
The prompt provides class definitions for Monster and Team:
` class Monster { String name; // Unique identifier of the monster. int health; // Initial hit points. int attack; // Damage inflicted when attacking. ... }
class Team { String label; // Team identifier, either "A" or "B". List<Monster> monsters; // Monsters listed in spawn order. } `
The task is to implement the method simulateBattle(teamA, teamB) that runs the battle simulation and returns a list of event strings based on these rules:
[ATTACK] event.[ATTACK] event.[DEATH] event. The next surviving monster becomes the new active monster.[RESULT] event declaring the winning team.Event Formats:
[ATTACK] <attacker_team>:<attacker_name> -> <defender_team>:<defender_name> | DMG=<damage> | HP:<before> -> <after>[DEATH] <team>:<monster_name> defeated | NEXT=<next_monster_label_or_NONE>[RESULT] WINNER=<team>` class Monster: def init(self, name, health, attack): self.name = name self.health = health self.attack = attack
class Team: def init(self, label, monsters): self.label = label self.monsters = monsters self.activeIdx = 0
def getActive(self):
if self.activeIdx < len(self.monsters):
return self.monsters[self.activeIdx]
return None
def advanceAndNextLabel(self):
self.activeIdx = self.activeIdx + 1
if self.activeIdx < len(self.monsters):
return self.label + ":" + self.monsters[self.activeIdx].name
return "NONE"
def hasAlive(self):
return self.activeIdx < len(self.monsters)
class Solution: def simulateBattle(self, teamA, teamB): log = []
while teamA.hasAlive() and teamB.hasAlive():
attacker = teamA.getActive()
defender = teamB.getActive()
# Attacks
hpBefore = defender.health
defender.health = max(0, defender.health - attacker.attack)
log.append(f"[ATTACK] A:{attacker.name} -> B:{defender.name} | DMG={attacker.attack} | HP:{hpBefore} -> {defender.health}")
if defender.health <= 0:
nextLabel = teamB.advanceAndNextLabel()
log.append(f"[DEATH] B:{defender.name} defeated | NEXT={nextLabel}")
continue # defeated defender cannot counterattack
# Counterattacks
hpBeforeA = attacker.health
attacker.health = max(0, attacker.health - defender.attack)
log.append(f"[ATTACK] B:{defender.name} -> A:{attacker.name} | DMG={defender.attack} | HP:{hpBeforeA} -> {attacker.health}")
if attacker.health <= 0:
nextLabel = teamA.advanceAndNextLabel()
log.append(f"[DEATH] A:{attacker.name} defeated | NEXT={nextLabel}")
winner = "A" if teamA.hasAlive() else "B"
log.append("[RESULT] WINNER=" + winner)
return log
def test1(): print("===== Test 1 =====")
solution = Solution()
monster1 = Monster("Dragon", 10, 5)
monster2 = Monster("Goblin", 12, 3)
teamA = Team("A", [monster1])
teamB = Team("B", [monster2])
log = solution.simulateBattle(teamA, teamB)
print(log)
# Expected:
# [
# "[ATTACK] A:Dragon -> B:Goblin | DMG=5 | HP:12 -> 7",
# "[ATTACK] B:Goblin -> A:Dragon | DMG=3 | HP:10 -> 7",
# "[ATTACK] A:Dragon -> B:Goblin | DMG=5 | HP:7 -> 2",
# "[ATTACK] B:Goblin -> A:Dragon | DMG=3 | HP:7 -> 4",
# "[ATTACK] A:Dragon -> B:Goblin | DMG=5 | HP:2 -> 0",
# "[DEATH] B:Goblin defeated | NEXT=NONE",
# "[RESULT] WINNER=A"
# ]
def test2(): print("===== Test 2 =====")
solution = Solution()
monster1 = Monster("Warrior", 5, 4)
monster2 = Monster("Goblin", 3, 1)
monster3 = Monster("Orc", 10, 2)
teamA = Team("A", [monster1])
teamB = Team("B", [monster2, monster3])
log = solution.simulateBattle(teamA, teamB)
print(log)
# Expected:
# [
# "[ATTACK] A:Warrior -> B:Goblin | DMG=4 | HP:3 -> 0"
# "[DEATH] B:Goblin defeated | NEXT=B:Orc"
# "[ATTACK] A:Warrior -> B:Orc | DMG=4 | HP:10 -> 6"
# "[ATTACK] B:Orc -> A:Warrior | DMG=2 | HP:5 -> 3"
# "[ATTACK] A:Warrior -> B:Orc | DMG=4 | HP:6 -> 2"
# "[ATTACK] B:Orc -> A:Warrior | DMG=2 | HP:3 -> 1"
# "[ATTACK] A:Warrior -> B:Orc | DMG=4 | HP:2 -> 0"
# "[DEATH] B:Orc defeated | NEXT=NONE"
# "[RESULT] WINNER=A"
# ]
def test3(): print("===== Test 3 =====")
solution = Solution()
monster1 = Monster("Knight", 10, 3)
monster2 = Monster("Mage", 6, 5)
monster3 = Monster("Goblin", 4, 2)
monster4 = Monster("Troll", 15, 4)
monster5 = Monster("Dragon", 8, 6)
teamA = Team("A", [monster1, monster2])
teamB = Team("B", [monster3, monster4, monster5])
log = solution.simulateBattle(teamA, teamB)
print(log)
# Expected:
# [
# "[ATTACK] A:Knight -> B:Goblin | DMG=3 | HP:4 -> 1",
# "[ATTACK] B:Goblin -> A:Knight | DMG=2 | HP:10 -> 8",
# "[ATTACK] A:Knight -> B:Goblin | DMG=3 | HP:1 -> 0",
# "[DEATH] B:Goblin defeated | NEXT=B:Troll",
# "[ATTACK] A:Knight -> B:Troll | DMG=3 | HP:15 -> 12",
# "[ATTACK] B:Troll -> A:Knight | DMG=4 | HP:8 -> 4",
# "[ATTACK] A:Knight -> B:Troll | DMG=3 | HP:12 -> 9",
# "[ATTACK] B:Troll -> A:Knight | DMG=4 | HP:4 -> 0",
# "[DEATH] A:Knight defeated | NEXT=A:Mage",
# "[ATTACK] A:Mage -> B:Troll | DMG=5 | HP:9 -> 4",
# "[ATTACK] B:Troll -> A:Mage | DMG=4 | HP:6 -> 2",
# "[ATTACK] A:Mage -> B:Troll | DMG=5 | HP:4 -> 0",
# "[DEATH] B:Troll defeated | NEXT=B:Dragon",
# "[ATTACK] A:Mage -> B:Dragon | DMG=5 | HP:8 -> 3",
# "[ATTACK] B:Dragon -> A:Mage | DMG=6 | HP:2 -> 0",
# "[DEATH] A:Mage defeated | NEXT=NONE",
# "[RESULT] WINNER=B",
# ]
def test4(): print("===== Test 4 =====")
solution = Solution()
monster1 = Monster("Dragon", 20, 5)
teamA = Team("A", [monster1])
teamB = Team("B", [])
log = solution.simulateBattle(teamA, teamB)
print(log)
# Expected:
# [
# "[RESULT] WINNER=A"
# ]
if name == "main": test1() test2() test3() test4() `