Google Interview experience
4 rounds over 4 weeks. No offer, good experience.
This round had me solving a pretty standard coding problem - nothing too crazy. I had to come up with an optimal solution and explain my thought process. Overall, it was straightforward and the interviewer seemed happy with my approach.
Given a string containing placeholders in the format %WORD%, you need to replace each placeholder with the corresponding value from a dictionary. If a placeholder is not found in the dictionary, it can be left unchanged or replaced with an empty string, depending on the specific requirements.
Example:
Input String: "Hello %NAME%, welcome to %PLACE%!"
Dictionary: {"NAME": "Alice", "PLACE": "Wonderland"}
Output String: "Hello Alice, welcome to Wonderland!"
Traverse the String: Iterate through the string character by character.
Identify Placeholders: Detect placeholders by checking for the % character and extract the word between % symbols.
Replace Placeholders: Use the dictionary to replace the placeholders with their corresponding values or keep them unchanged if not found.
import java.util.HashMap;
import java.util.Map;
public class PlaceholderReplacer {
public static String replacePlaceholders(String text, Map<String, String> variables) {
StringBuilder result = new StringBuilder();
int length = text.length();
int i = 0;
while (i < length) {
if (text.charAt(i) == '%') {
int start = i + 1;
// Find the end of the placeholder
while (i < length && text.charAt(i) != '%') {
i++;
}
if (i < length) {
String key = text.substring(start, i);
String replacement = variables.getOrDefault(key, "%" + key + "%");
result.append(replacement);
i++; // Skip the ending '%'
}
} else {
result.append(text.charAt(i));
}
i++;
}
return result.toString();
}
public static void main(String[] args) {
String text = "Hello %NAME%, welcome to %PLACE%!";
Map<String, String> variables = new HashMap<>();
variables.put("NAME", "Alice");
variables.put("PLACE", "Wonderland");
String result = replacePlaceholders(text, variables);
System.out.println(result); // Output: "Hello Alice, welcome to Wonderland!"
}
}Time Complexity:
O(n)
Finding placeholders: O(n)
Replacing placeholders: O(n) for scanning and replacement.
The coding question in this round focused on problem-solving, edge case handling, and code optimization.
Generate all possible valid sequences of musical notes where:
Each sequence sums to exactly 12.
Notes can only be 1, 2, or 3.
Valid transitions between notes are given by the dictionary {1: [2, 3], 2: [1, 2], 3: [1]} (e.g., after 1, you can only have 2 or 3).
The first and last notes in the sequence must have a valid transition according to the dictionary.
allowed: [1, 2, 2, 2, 1, 1, 3] (First note 1 can be followed by 2, and last note 3 can follow 1)
not allowed: [1, 2, 2, 2, 2, 2, 1] (First note 1 can be followed by 2, but last note 1 cannot follow 1)
Return all possible valid sequences.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MusicalNotes {
private static final int TARGET_SUM = 12;
private static final int MAX_LENGTH = 11;
private static final Map<Integer, List<Integer>> transitions = new HashMap<>();
static {
transitions.put(1, List.of(2, 3));
transitions.put(2, List.of(1, 2));
transitions.put(3, List.of(1));
}
public List<List<Integer>> findSequences() {
List<List<Integer>> results = new ArrayList<>();
dfs(new ArrayList<>(), 0, results);
return results;
}
private void dfs(List<Integer> current, int currentSum, List<List<Integer>> results) {
if (currentSum > TARGET_SUM || current.size() > MAX_LENGTH) {
return; // Prune invalid sequences
}
if (currentSum == TARGET_SUM && current.size() > 1) {
// Check valid transition between first and last note
if (isValidTransition(current.get(0), current.get(current.size() - 1))) {
results.add(new ArrayList<>(current));
}
return;
}
for (int note : transitions.keySet()) {
if (current.isEmpty() || transitions.get(current.get(current.size() - 1)).contains(note)) {
current.add(note);
dfs(current, currentSum + note, results);
current.remove(current.size() - 1); // Backtrack
}
}
}
private boolean isValidTransition(int start, int end) {
return transitions.get(start).contains(end);
}
public static void main(String[] args) {
MusicalNotes musicalNotes = new MusicalNotes();
List<List<Integer>> sequences = musicalNotes.findSequences();
System.out.println("Possible sequences:");
for (List<Integer> sequence : sequences) {
System.out.println(sequence);
}
}
}findSequences Method: Starts the search for valid sequences.
dfs Method: Recursively builds sequences by adding notes, ensuring the sum is 12 and the length does not exceed 11. It checks each sequence for validity based on transitions and constraints.
Pruning: Sequences are pruned if the sum exceeds 12 or the length exceeds 11, improving efficiency.
The time complexity is O(n^L), where n is the number of possible notes (3) and L is the maximum length of the sequence (12). In this case:
Depth of Recursion: Up to 11 levels (max length).
Branching Factor: Up to 3 possible notes per level.
The question was of medium difficulty but required handling multiple cases which made it a bit long and messy. The interviewer wasn’t super engaged, so it felt like I was on my own for most of the interview.
Was asked to implement a function to handle three types of queries related to employee management:
manager, a, b: Indicates that employee a is the manager of employee b.
peer, a, b: Checks if employees a and b have the same manager.
is_manager, a, b: Determines if employee a is the manager of employee b.
Can assume that each input is valid and each query is processed one at a time. It's possible for a peer query to pass two employees who do not currently have any manager assigned.
import java.util.HashMap;
import java.util.Map;
public class EmployeeManagement {
private Map<String, String> managerMap;
public EmployeeManagement() {
managerMap = new HashMap<>();
}
public String handleQuery(String query) {
String[] parts = query.split(", ");
String command = parts[0];
switch (command) {
case "manager":
// Set employee `b`'s manager to `a`
String manager = parts[1];
String employee = parts[2];
managerMap.put(employee, manager);
return null; // No output for 'manager' command
case "peer":
// Check if both `a` and `b` have the same manager
String a = parts[1];
String b = parts[2];
String managerA = managerMap.get(a);
String managerB = managerMap.get(b);
// Check if both have the same manager or both have no manager
if (managerA == null && managerB == null) {
return "true"; // Both are managerless
}
if (managerA != null && managerA.equals(managerB)) {
return "true";
}
return "false";
case "is_manager":
// Check if `a` is the manager of `b`
String potentialManager = parts[1];
String employeeQuery = parts[2];
String actualManager = managerMap.get(employeeQuery);
return Boolean.toString(potentialManager.equals(actualManager));
default:
throw new IllegalArgumentException("Unknown command: " + command);
}
}
public static void main(String[] args) {
EmployeeManagement management = new EmployeeManagement();
// Example queries
System.out.println(management.handleQuery("manager, Alice, Bob")); // null
System.out.println(management.handleQuery("is_manager, Alice, Bob")); // true
System.out.println(management.handleQuery("peer, Alice, Bob")); // false
System.out.println(management.handleQuery("peer, Charlie, Dave")); // true (both managerless)
}
}Last interview was the Googleyness and leadership interview, discussing how I've handled difficult team dynamics or made decisions with incomplete information/ambiguity
Know someone interviewing at Google? Pass it on.
Same company, different candidates.
Share yours while it’s fresh. It takes five minutes, it’s anonymous, and it will help future candidates.