Google Interview- Round 1
Company: Google
Position: Software Engineer III, Google Cloud - India [Frontend]
Time limit: 45 mins exactly.
Medium: Google Meet & Google Docs for coding
Current Position: Senior Software Engineer(Frontend) in a MNC
Recruiter reached me out through LinkedIn.
Question:
Given a time (in 24-hour format) with missing digits marked as '?', we want to replace all of the question marks with digits (0-9) in such a way as to obtain a valid time. The earliest possible time is 00:00 and the latest valid time is 23:59.
Write a function:
class Solution { public int solution(String T); }
that, given a string T in the format "HH:MM", returns an integer denoting the number of valid times that can be obtained by replacing the question marks.
Examples:
Given T = "2?:45", the function should return 4. We can obtain four valid times: "20:45", "21:45", "22:45" and "23:45".
Given T = "?9:32", the function should return 2. Valid times are: "09:32" and "19:32".
Given T = "0?:?0", the function should return 60.
Given T = "?4:0?", the function should return 20.
Given T = "29:01", the function should return 0.
Assume that: T consists of exactly five characters; the third one is ':'; the others are digits (0-9) or '?'.
In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.
The code I provided:
def valid_times(T): // make string T as input
def_is_valid(time):
hours, minutes = int(time[:2]), int(time[3: ])
Return 0 < = hours < 24 and 0 <= minutes <60
Count =0
for h1 in (‘0’, ‘1’, ‘2’) if [0] == ‘?’ else (T[0],):
for h2 in (‘0’, ‘1’, ‘2’, ‘3’) if h1 == ‘2’ else (‘0’,’1’, ‘2’, ‘3’...’9’):
for m1 in (‘5’ if T[3] == ‘?’ else T[3]:
// for m1 in (‘0’,’1’,...’5’) if T[3] == ‘?’ else (T[3],):
for m2 in (‘0’,’1’, ‘2’, ‘3’...’9’):
time = h1+ (h2 if T[1] == ‘?’ else T[1] + ‘:’ + m1 + m2
if is_valid(time):
Count += 1
return count
#Test
print(valid_times(“23:45”)) # 4Please note: The code I wrote above was in hurry and I might have made some mistake eg: hardcoded values
Feedback: She was satisfied with my solution and the way I explained the interviewer however she gave me some inputs.
I hope this helps, this was specifically for frontend position. However, they wanted to check DSA skills as well.