Find Optimal Input
Updated
Problem statement
A microwave accepts a numeric input and a target cooking time. Find the optimal input under the rules below.
- Legal inputs are integers from
0through9999, written without leading zeros. - A one- or two-digit input is interpreted directly as seconds. For example,
81means81seconds. - 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,999means9minutes99seconds, and1221means12minutes21seconds. - Every key press costs
1. Moving between two different consecutive keys costs an additional2; repeating the same key has no movement cost. - The interpreted time must be within an inclusive
10%oftargetTime. - 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 = 888Input 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