Skip to main content
All Questions

Implement a CLI number guessing game with bulls and cows.

Unlock detailed company stats for this questionUpgrade

The user must guess a 4-digit number without repeated digits and has 7 attempts. Every guess must be a 4-digit number. They either win (guess correctly), lose (7th attempt wrong) or get 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. If the number is 1234, then 1290 is 2 bulls and 0 cows, 1234 wins and 1562 is 1 bull and 1 cow. It should be a CLI program. 0123 is invalid (cannot start with 0), 1023 is valid, 1123 is invalid (repeated digits). Each new game gets a random number. Bonus: print "Gutter ball!!!" on 0 bulls and 0 cows, and never repeat the same number across games. Focus on setting up test cases first and be ready to write a unit test class.

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.