Google Interview Question
Software EngineersCountry: United States
Interview Type: Written Test
Algorithm:
String longestSubstring = "";
Create two collections:
Distinct characters collection
Duplicate characters collection
Create two pointers:
Start index
End index
Loop
While end index does not reach the end of string( increment end index)
if(distinct_collection.size() < k)
Read character at end index
If(char is in distinct)
Remove char from distinct collection + Add to duplicate collection
else if (char in duplicate )
Do nothing
// This means the char has come up the first time
else
Add to distinct collection
// This will happen when the number of distinct characters == k
else
If substring(startIndex, endIndex).length() > longestSubstring.length()
longestSubstring = substring(startIndex, endIndex)
Loop (distinct collection.size() == k)
If character at startPtr in distinct collection
startPtr++
Remove character from distinct collection
Else
continue;
}
Print longestSubstring
String longestSubstring = "";
Create two collections:
Distinct characters collection
Duplicate characters collection
Create two pointers:
Start index
End index
Loop
While end index does not reach the end of string( increment end index)
if(distinct_collection.size() < k)
Read character at end index
If(char is in distinct)
Remove char from distinct collection + Add to duplicate collection
else if (char in duplicate )
Do nothing
// This means the char has come up the first time
else
Add to distinct collection
// This will happen when the number of distinct characters == k
else
If substring(startIndex, endIndex).length() > longestSubstring.length()
longestSubstring = substring(startIndex, endIndex)
Loop (distinct collection.size() == k)
If character at startPtr in distinct collection
startPtr++
Remove character from distinct collection
Else
continue;
}
Print longestSubstring
Solution in python for the round 1
from random import choice
from string import ascii_lowercase
def stream_of_chars():
while True:
yield choice(ascii_lowercase)
def longest_unique_str(k, max_rounds=1000):
unique_str, max_unique_str = list(), list()
repeated = set()
for round, char in enumerate(stream_of_chars()):
# Maximum number of characters that we can consume from the stream
if round == max_rounds:
break
# If it is a unique character just add it to the list
if char not in repeated:
repeated.add(char)
unique_str.append(char)
if len(unique_str) == k:
return unique_str
else:
# Check if it is a maximal string
if len(unique_str) > len(max_unique_str):
max_unique_str = unique_str
# Find where the repeated character appears on the string
index = unique_str.index(char)
# Update the repeated values and the list of values removing
# the characters that appear before the repeated character
for c in unique_str[:index+1]:
repeated.remove(c)
unique_str = unique_str[index+1:]
return max_unique_str
Looking for interview experience sharing and coaching?
Visit aonecode.com for private lessons by FB, Google and Uber engineers
Our ONE TO ONE class offers
SYSTEM DESIGN Courses (highly recommended for candidates for FLAG & U)
ALGORITHMS (conquer DP, Greedy, Graph, Advanced Algos & Clean Coding),
latest interview questions sorted by companies,
mock interviews.
Our students got hired from G, U, FB, Amazon, LinkedIn and other top tier companies after weeks of training.
Feel free to email us aonecoding@gmail.com with any questions. Thanks!
Solution to Round 4
- aonecoding July 23, 2017