Number Guessing Game - Problem

Create an interactive number guessing game where the computer generates a random number between 1 and 100, and the player tries to guess it.

Game Rules:

  • Generate a random number between 1-100 (inclusive)
  • Player enters guesses one by one
  • After each guess, provide feedback: "Too high", "Too low", or "Correct!"
  • When the correct number is guessed, display the total number of attempts
  • The game ends when the player guesses correctly

Note: For this implementation, you'll receive the target number and a list of guesses, then return the sequence of feedback messages ending with the attempt count.

Input & Output

Example 1 — Basic Game
$ Input: target = 67, guesses = [50, 80, 67]
Output: ["Too low", "Too high", "Correct!", "Attempts: 3"]
💡 Note: First guess 50 is too low, second guess 80 is too high, third guess 67 is correct. Game ends after 3 attempts.
Example 2 — Quick Win
$ Input: target = 42, guesses = [42]
Output: ["Correct!", "Attempts: 1"]
💡 Note: Lucky first guess! Player gets it right immediately in just 1 attempt.
Example 3 — Longer Game
$ Input: target = 25, guesses = [50, 30, 20, 25]
Output: ["Too high", "Too high", "Too low", "Correct!", "Attempts: 4"]
💡 Note: Player gradually narrows down: 50 too high, 30 too high, 20 too low, finally 25 is correct after 4 attempts.

Constraints

  • 1 ≤ target ≤ 100
  • 1 ≤ guesses.length ≤ 20
  • 1 ≤ guesses[i] ≤ 100

Visualization

Tap to expand
INPUTALGORITHMRESULTTarget: 67Guesses: [50, 80, 67]5080671Compare 50 vs 6750 < 67: Too low2Compare 80 vs 6780 > 67: Too high3Compare 67 vs 6767 = 67: Correct!4Add attempt countAttempts: 3Feedback Array:["Too low", "Too high", "Correct!", "Attempts: 3"]Game Complete!Key Insight: Each guess provides feedback that guides the player toward the correct answer, making the game interactive and engaging.TutorialsPoint - Number Guessing Game | Sequential Processing
Asked in
Google 15 Microsoft 12 Amazon 8
32.3K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen