Skip to main content
All Questions

Implement a CLI number guessing game with bulls and cows.

Unlock detailed company stats for this questionUpgrade

Build a CLI number guessing game (bulls and cows). The player must guess a 4-digit number with no repeated digits and has 7 attempts. Every guess must be a valid 4-digit number. Each guess results in a win (correct guess), a loss (7th attempt wrong) or a tip of X bulls and Y cows, where X is the number of correct digits in the correct positions and Y is the number of correct digits in wrong positions. Each new game gets a random number.

Examples, if the number is 1234:

  • 1290 is 2 bulls and 0 cows
  • 1234 wins
  • 1562 is 1 bull and 1 cow

Validity: 0123 is invalid (cannot start with 0), 1023 is valid, 1123 is invalid (repeated digits).

Bonus: print "Gutter ball!!!" on 0 bulls and 0 cows, and never repeat the same number across games.

Community answers

No answers contributed by the community yet.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.