Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Number Guessing Game

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Number Guessing Game. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Hudson River Trading's replay harness needs a deterministic simulation of an integer guessing game. Given a hidden target and an ordered sequence of guesses, report whether each guess is closer to the target than the previous guess.

The target is an integer from 0 through 1000. The first guess receives "start". For every later guess, compare its absolute distance from the target with the previous guess's distance:

  • Return "closer" if the distance decreases.
  • Return "farther" if the distance increases.
  • Return "same" if the distances are equal.
  • Return "correct" when a guess equals the target, and stop processing further guesses.

Formal Specification

Implement guess_feedback(target, guesses), where target is an integer and guesses is a non-empty list of integers. Return a list of strings containing one result for each processed guess. The first result is always "start" unless the first guess is correct, in which case it is "correct".

Examples

Example 1: target = 500, guesses = [100, 300, 450, 600] produces ["start", "closer", "closer", "farther"] because the distances are 400, 200, 50, and 100.

Example 2: target = 250, guesses = [0, 500, 250, 100] produces ["start", "same", "correct"]; processing stops at the exact guess.

Constraints

  • 0 <= target <= 1000
  • 1 <= len(guesses) <= 10^5
  • 0 <= guesses[i] <= 1000
  • The target may not appear in guesses

Function Signature

def guess_feedback(target, guesses):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output