Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Number Guessing Game

Basic Acceptance 89.45% Points 10

A number guessing game generates a secret number and gives the player a limited number of attempts to guess it.

You are given a secret number secret, a maximum number of attempts maxAttempts, and an array of guesses made by the player. Your task is to process each guess, provide the appropriate hint, and determine whether the player wins or loses.

After each incorrect guess, provide a hint:

  • Return Lower if the guess is greater than the secret number.
  • Return Higher if the guess is smaller than the secret number.
  • Return Win if the guess matches the secret number.

Stop the game when the player guesses correctly or runs out of attempts. Report the final result along with the number of attempts used.

Examples
Example 1

Input: secret = 42, maxAttempts = 5, guesses = [50, 25, 42]

Output: Lower, Higher, Win in 3 attempts

Explanation: The first guess, 50, is greater than 42, so the hint is Lower. The second guess, 25, is smaller than 42, so the hint is Higher. The third guess matches the secret number, resulting in a win after 3 attempts.

Example 2

Input: secret = 10, maxAttempts = 3, guesses = [10]

Output: Win in 1 attempt

Explanation: The first guess matches the secret number, so the player wins immediately using just 1 attempt.

Example 3

Input: secret = 75, maxAttempts = 2, guesses = [50, 60]

Output: Lose - attempts exhausted

Explanation: Both guesses are smaller than the secret number, so the hints are Higher and Higher. The player uses all 2 attempts without guessing the secret number and loses.

Constraints
  • 1 <= secret <= 100
  • 1 <= maxAttempts <= 100
Topics
Data Structures Basics Basics
Companies
Microsoft Coinbase
Added 28 Nov 2026 89.45% acceptance
Ln 1, Col 1Sign in to save your code
42 5 3
50 25 42
Lower, Higher, Win in 3 attempts