FastPrepFastPrep
Problem Brief

Find Optimal Input

ONSITE INTERVIEW
See Google online assessment and hiring insights

Given that a microwave takes key stroke inputs and a target cooking time, find the optimal input considering the following costs for input:

  • Input 999 will be interpreted as 9 minutes 99 seconds.
  • Input 81 as 81 seconds.
  • Input 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).

    Function Description

    Complete the function findOptimalInput in the editor.

    findOptimalInput has the following parameter:

    1. targetTime: the target cooking time in seconds

    Returns

    int: the optimal input value

    1Example 1

    Input
    targetTime = 600
    Output
    888
    Explanation

    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).

    public int findOptimalInput(int targetTime) {
      // write your code here
    }
    
    Input

    targetTime

    600

    Output

    888

    Sign in to submit your solution.