Google L4 1st Onsite round

Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2025-04-25Added By Anonymous
Source: Link

So just few minutes back I had my first onsite round with Google for L4 position(SWE-3), The interviewer was so cooperative and humble guy from Europe.


Asked me to built a program to substitute a string with given variables:

#{'X':'%Z%', 'Y':'45', 'Z':'32'}

#string='%X%_%Y%'

#output: 32_45

earlier he didn't mentioned about variables can refer to other variables too, but later he followed up with that on which I tweaked my solution to a recursive approach.

Good experience over all.


My solution: almost 90-95% like this:


As there was very few minutes left when he asked followup I missed cycle detection in this, though it was very minor change for cycle detection.

1class Solution: 2 def __init__(self, data): 3 self.data = data 4 5 def resolve_string(self, string_to_resolve, visited=None): 6 if visited is None: 7 visited = set() 8 9 if '%' not in string_to_resolve: 10 return string_to_resolve 11 12 var = '' 13 start = 0 14 n = len(string_to_resolve) 15 res_str = '' 16 var_part = False 17 18 while start < n: 19 if not var_part and string_to_resolve[start] == '%': 20 var_part = True 21 var = '' 22 elif var_part and string_to_resolve[start] != '%': 23 var += string_to_resolve[start] 24 elif var_part and string_to_resolve[start] == '%': 25 var_part = False 26 if var in visited: 27 raise ValueError(f"Cyclic reference detected: {' -> '.join(visited)} -> {var}") 28 visited.add(var) 29 resolved = self.resolve_string(self.data.get(var, f"%{var}%"), visited) 30 visited.remove(var) 31 res_str += resolved 32 elif not var_part: 33 res_str += string_to_resolve[start] 34 start += 1 35 36 return res_str 37
Comments