Rock Paper Scissors Winner
Given two moves (rock, paper, or scissors), return which player wins or whether the round is a draw.
If both moves are equal, it is a draw. Otherwise apply the three winning rules: rock beats scissors, scissors beats paper, and paper beats rock. If player one's move beats player two's, player one wins; otherwise player two wins.
Problem Statement
Two players each choose one of rock, paper, or scissors. Given both
moves, decide the outcome of the round.
The rules are: rock beats scissors, scissors beats paper, and paper beats rock.
If both players pick the same move it is a draw. Return Player 1 wins,
Player 2 wins, or Draw. For example, rock versus scissors gives
Player 1 wins.
Input: Two strings, move1 and move2, each one of "rock", "paper", or "scissors".
Output: One of the strings: "Player 1 wins", "Player 2 wins", or "Draw".
Examples
Input: move1="rock", move2="scissors"
Output: Player 1 winsRock beats scissors, so player 1 takes the round.
Input: move1="paper", move2="paper"
Output: DrawIdentical moves always result in a draw.
Constraints
Each move is exactly one of rock, paper, or scissors (lowercase)Exactly two moves are given per round
Think Before You Code
Reveal the questions to ask yourself first
- What is the simplest case to detect first — when does neither player win?
- How many distinct winning combinations are there for player one?
- If player one does not win and it is not a draw, what must the result be?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Resolve the round with a short decision chain.
- If
move1equalsmove2, returnDraw. - Otherwise check the three combinations where player one wins:
- rock vs scissors
- scissors vs paper
- paper vs rock
If any match, return
Player 1 wins.
- If it was not a draw and player one did not win, return
Player 2 wins.
Because a non-draw round always has exactly one winner, once you have ruled out the draw and player one's wins, player two is the only remaining outcome.
Dry Run
Walk through the example step by step
Resolving move1 = "rock", move2 = "scissors":
step | result
-----------------------------+-----------------
move1.equals(move2)? | "rock" vs "scissors" -> no
rock beats scissors? | yes
return | "Player 1 wins"
Solution
Reveal the full Java solution
public class RockPaperScissors {
public static String winner(String move1, String move2) {
if (move1.equals(move2)) {
return "Draw";
}
boolean p1Wins =
(move1.equals("rock") && move2.equals("scissors")) ||
(move1.equals("scissors") && move2.equals("paper")) ||
(move1.equals("paper") && move2.equals("rock"));
return p1Wins ? "Player 1 wins" : "Player 2 wins";
}
public static void main(String[] args) {
System.out.println(winner("rock", "scissors")); // Player 1 wins
System.out.println(winner("paper", "paper")); // Draw
}
}
The three winning combinations for player one fully describe the game's rules;
everything else is either a draw or a loss for player one. By testing the draw
first and player one's wins second, the final else cleanly covers every case
where player two wins, with no combination missed or double-counted.
Using equals (not ==) is essential because the moves are String objects
and must be compared by content, not by reference.
O(1)Space: O(1)Common Mistakes
- Comparing the move strings with == instead of equals, giving wrong results for non-interned strings.
- Listing an incomplete or incorrect rule set, e.g. thinking rock beats paper.
Edge Cases to Test
- Both players choosing the same move returns Draw.
- Each of the three winning pairs for player one must be recognised.
- The reversed pairs (e.g. scissors vs rock) correctly fall through to Player 2 wins.
Interview Follow-Ups
- How would you extend this to Rock Paper Scissors Lizard Spock with five moves?
- How would you validate that each input is one of the three legal moves?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →