algorithm
number guessing game
coding challenge
game strategy
computer science

How to approach a number guessing game with a twist algorithm?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

A normal number guessing game is solved with binary search because each answer cleanly cuts the search space in half. The interesting part starts when the game adds a twist, such as allowing the responder to lie once, because then you need to track not one candidate interval but the set of values still consistent with the answers so far.

Core Sections

Start with the ordinary case

If the hidden number is between 1 and n and every response is truthful, the optimal strategy is binary search. Ask about the midpoint, then keep either the lower or upper half depending on whether the answer is “too low” or “too high.”

python
1def binary_guess_steps(low, high):
2    steps = []
3    while low <= high:
4        mid = (low + high) // 2
5        steps.append(mid)
6        low = mid + 1
7    return steps

The exact interaction depends on the game rules, but the principle is the same: every truthful comparison eliminates half the remaining possibilities.

A useful twist: one answer may be false

Suppose the responder may lie once during the whole game. Now a single “too low” answer does not prove all lower values are impossible, because that answer itself might be the lie.

A clean way to model this is to keep two candidate sets:

  • numbers consistent with all answers if no lie has happened yet
  • numbers consistent with all answers if exactly one lie has happened already

After each answer, update both sets.

Update the candidate sets after each guess

Here is a simple simulation approach in Python. It is not the most memory-efficient possible implementation, but it is clear and runnable.

python
1def update_candidates(no_lie, one_lie, guess, answer):
2    def consistent(value, ans):
3        if ans == "low":
4            return guess < value
5        if ans == "high":
6            return guess > value
7        if ans == "correct":
8            return guess == value
9        raise ValueError("unknown answer")
10
11    new_no_lie = {v for v in no_lie if consistent(v, answer)}
12
13    lied_from_no_lie = {v for v in no_lie if not consistent(v, answer)}
14    stayed_one_lie = {v for v in one_lie if consistent(v, answer)}
15    new_one_lie = lied_from_no_lie | stayed_one_lie
16
17    return new_no_lie, new_one_lie

If an answer is truthful, candidates stay in the same bucket. If the answer is the one lie, candidates move from the “no lie yet” bucket into the “one lie used” bucket.

Drive the game state

python
1def solve_with_one_possible_lie(n, interactions):
2    no_lie = set(range(1, n + 1))
3    one_lie = set()
4
5    for guess, answer in interactions:
6        no_lie, one_lie = update_candidates(no_lie, one_lie, guess, answer)
7
8    return sorted(no_lie | one_lie)
9
10
11history = [
12    (50, "low"),
13    (75, "high"),
14    (62, "low"),
15]
16print(solve_with_one_possible_lie(100, history))

The final list contains numbers still possible after accounting for up to one false answer.

How to choose the next guess

In the standard game, the midpoint is obviously best. With a twist, the best next guess is the one that splits the remaining candidates as evenly as possible across the possible answer states. For a coding challenge, a good heuristic is:

  1. compute the current candidate set
  2. test a few possible guesses
  3. choose the guess that minimizes the largest remaining branch

That idea generalizes beyond one-lie games. Whenever the feedback is noisy or constrained, good strategies come from minimizing worst-case remaining uncertainty rather than blindly taking the numeric midpoint.

Match the algorithm to the actual twist

The exact algorithm changes completely if the twist is different. Examples:

  • if the number can move after each guess, you need state transition modeling
  • if answers are “warmer” or “colder,” you compare relative distances
  • if guesses have a cost, you optimize expected cost rather than number of questions

So the first real step is not coding. It is writing down what the twist actually changes about the information you receive.

Common Pitfalls

  • Reusing plain binary search even after the twist breaks the assumption that every answer is perfectly truthful.
  • Starting implementation before defining the game state precisely, which leads to an algorithm that solves the wrong problem.
  • Tracking only one interval when the rules require multiple possible worlds, such as “no lie yet” and “one lie already used.”
  • Optimizing average-case behavior when the puzzle is really asking for a guaranteed worst-case strategy.
  • Confusing the midpoint of the numeric range with the best split of the remaining logical candidate set.

Summary

  • Standard number guessing uses binary search because each truthful answer halves the search space.
  • Once the game adds a twist, the right model depends on what information can become unreliable.
  • For a one-lie variant, track candidate numbers under both “no lie yet” and “lie already used” states.
  • Choose guesses based on uncertainty reduction, not just the numeric midpoint.
  • Define the rules precisely before selecting the algorithm, because different twists need different state models.

Course illustration
Course illustration

All Rights Reserved.