Given that a microwave takes key stroke inputs and a target cooking time, find the optimal input considering the following costs for input:
999 will be interpreted as 9 minutes 99 seconds.81 as 81 seconds.1221 as 12 minutes 21 seconds.
The cost of each key stroke is 1, and the cost of moving the finger to a different key is 2. For example, input 999 has a cost of 3, input 1122 has a cost of 6, and input 1234 has a cost of 10.
The input has to be within 10% of the target time. If the cost is the same, select the input that's closest to the target time.
For example, for a target time of 10 minutes, 888 is the optimal input (not 999).
Complete the function findOptimalInput in the editor.
findOptimalInput has the following parameter:
targetTime: the target cooking time in seconds
Returns
int: the optimal input value
targetTime = 600 return = 888
For a target time of 10 minutes (600 seconds), the optimal input is 888, which is interpreted as 8 minutes 88 seconds or 528 seconds. This is within 10% of the target time and has a lower cost than 999 (which would be 9 minutes 99 seconds or 599 seconds).
- Periodic Table Word FormationsPHONE SCREEN · Seen Jun 2026
- Fountain SafetyONSITE INTERVIEW · Seen Jun 2026
- Consolidate On-Call RotationsOA · Seen Jun 2026
- Detonate Bombs with Chain ReactionsONSITE INTERVIEW · Seen May 2026
- Evaluate a Nested Math ExpressionONSITE INTERVIEW · Seen May 2026
- Tic-Tac-Toe Game StatusPHONE SCREEN · Seen May 2026
- Longest Dictionary TokenizationPHONE SCREEN · Seen May 2026
- Minimum Cars for Rental RequestsONSITE INTERVIEW · Seen Apr 2026
public int findOptimalInput(int targetTime) {
// write your code here
}