FastPrepFind Optimal Input
Problems/Find Optimal Input

Find Optimal Input

Google logoGoogle● MediumNEW GRADPHONE SCREENONSITE INTERVIEW

Updated Oct 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: 2026-07-17 — The source leaves entry length, leading zeros, and the final tie-break unspecified. For stable judging, this version checks integer inputs from 0 to 9999, disallows leading zeros, and chooses the smaller input when both cost and time difference tie. The judged core task matches the visible source at about 97%.

A microwave accepts a numeric input and a target cooking time. Find the optimal input under the rules below.

  • Legal inputs are integers from 0 through 9999, written without leading zeros.
  • A one- or two-digit input is interpreted directly as seconds. For example, 81 means 81 seconds.
  • For a three- or four-digit input, all but the last two digits are minutes and the final two digits are seconds. The seconds part may exceed 59. For example, 999 means 9 minutes 99 seconds, and 1221 means 12 minutes 21 seconds.
  • Every key press costs 1. Moving between two different consecutive keys costs an additional 2; repeating the same key has no movement cost.
  • The interpreted time must be within an inclusive 10% of targetTime.
  • Choose the candidate with the lowest entry cost. If costs tie, choose the one closest to targetTime. If both cost and difference tie, choose the smaller numeric input.

For a target of 600 seconds, 888 means 568 seconds and costs 3, while 999 means 639 seconds and also costs 3. Since 568 is closer to 600, the optimal input is 888.

Function

findOptimalInput(targetTime: int) → int

Complete findOptimalInput, which receives targetTime in seconds and returns the optimal numeric input.

Examples

Example 1

targetTime = 600return = 888

Input 888 means 8 minutes 88 seconds, or 568 seconds. Input 999 means 9 minutes 99 seconds, or 639 seconds. Both cost 3 and are within 10% of 600, but 888 is closer by absolute time difference (32 seconds versus 39 seconds).

Constraints

  • 1 <= targetTime <= 6039
  • Candidate inputs are integers in [0, 9999] with no leading zeros.
  • The inclusive tolerance test is abs(actualTime - targetTime) * 10 <= targetTime.

More Google Onsite Interview questions

  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
  • Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
  • Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Climb Detection with Recoverable DipsStrava · PHONE SCREEN
  • UCB1 Multi-Armed Bandit TraceWayfair · ONSITE INTERVIEWWayfair interview questions
  • Evenly Schedule Vehicle InspectionsAmazon · PHONE SCREEN
  • Experiment Bucket Allocation and RebalancingPinterest · PHONE SCREENPinterest interview questions
  • API Call Thread Pool ScheduleBaseten · ONSITE INTERVIEWBaseten interview questions
  • Streaming Ingredient DishesHarness · ONSITE INTERVIEW
  • Automation Pipeline, Part 3: Stage DurationPlaid · PHONE SCREENPlaid interview questions
  • Balanced Human, Model and Task AssignmentOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
  • Choose the Next Hidden-Word GuessFlexport · ONSITE INTERVIEWFlexport interview questions
  • Generate an Optimal Portfolio Trading ReportPoint72 · PHONE SCREENPoint72 interview questions

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int findOptimalInput(int targetTime) {
      // write your code here
    }

}
Checking account…