Simulate Multiplayer War
Updated
Problem statement
Simulate a bounded game of War. hands[i] is player i's queue, with the next card at index 0. Higher ranks beat lower ranks. Return the only player still holding cards after at most maxBattles complete battles, or -1 if no unique winner is established.
For this exercise, use these deterministic rules:
- At the start of a battle, every player with cards reveals one card. Process players in increasing index order, appending each revealed card to a shared pot.
- If exactly one revealed card has the highest rank, its player appends the entire pot to the back of their queue in the order cards entered the pot. The battle ends.
- If the highest rank is tied, a war begins. Every player who still has cards participates, including players who revealed a lower rank earlier. In increasing player-index order, a player with at least four remaining cards adds three face-down cards and then one face-up card to the pot, in queue order. A player with fewer than four remaining cards instead adds all remaining cards to the pot and reveals no face-up card.
- Compare only the face-up cards of that war stage. A unique highest player wins the pot. A tie repeats step 3 with every player who still has cards. If no face-up card is revealed, return
-1.
An entire sequence of tied war stages counts as one battle. If initially exactly one player has cards, return that player even when the limit is zero. Return -1 for an initially empty game or when several players retain cards after the battle limit. Ranks may repeat without restriction; suits and standard-deck composition are not modeled.
Function
warWinner(hands: int[][], maxBattles: int) → intExamples
Example 1
hands = [[2],[3]]maxBattles = 1return = 1Both players reveal their only card. Rank 3 wins both cards, leaving only player 1.
Example 2
hands = [[5,2,3,4,9],[5,6,7,8,10]]maxBattles = 1return = 1The initial fives tie. Each player puts three cards face down, then player 1 reveals 10 against 9 and wins all ten cards.
Example 3
hands = [[8],[8]]maxBattles = 5return = -1The initial eights tie, but nobody has another face-up card. The war cannot determine a winner.
Constraints
2 <= hands.length <= 6.0 <= hands[i].length <= 40.2 <= hands[i][j] <= 14.0 <= maxBattles <= 10000.
More Palantir interview questions
- Map and Accumulate Learning QueriesONSITE INTERVIEW · Seen Oct 2026
- Relational Learning QueriesONSITE INTERVIEW · Seen Apr 2025
- Find Unmet Users With Shared InterestsONSITE INTERVIEW · Seen Mar 2025
- Maximum Profit with Repeated Stock TradesONSITE INTERVIEW · Seen Sep 2024
- Minimum Calls for Repeated API LookupsONSITE INTERVIEW · Seen Oct 2022
- Find Cookable RecipesONSITE INTERVIEW · Seen Nov 2019
- Trace Terminal Company OwnershipONSITE INTERVIEW · Seen Nov 2019
- Query Words Within K PositionsPHONE SCREEN · Seen Nov 2026
- Basic Item DiscountsOA · Seen Nov 2026
- Optimize Discount Selection Under a Point BudgetOA · Seen Nov 2026
Similar questions at other companies
- Five-Minute Hit CounterGoogle · ONSITE INTERVIEWGoogle interview questions
- Sliding-Window Rate LimiterSnowflake · ONSITE INTERVIEWSnowflake interview questions
- Bounded Token-Bucket Request QueuePlaid · ONSITE INTERVIEWPlaid interview questions
- Paginate Provider Listings with Per-Page DiversityMaven Clinic · ONSITE INTERVIEWMaven Clinic interview questions
- Ring Buffer OperationsAmazon · ONSITE INTERVIEW
- Recent Hit CounterAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Team-Preferred Stable MatchingBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- Realized PnL with Oldest Inventory FirstBelvedere Trading · OA
- Implement a FIFO QueueByteDance · PHONE SCREENByteDance interview questions