Google L5 Phone interview | Reject
Company: Google
Role: L5
Interview Stage: Phone Screen
Location: unknown
Year: unknown
Original Post Content:
questions[
{id:1, tags: ["MAC", "VSCODE"]},
{id:2, tags: ["PY", "AI"]},
{id:3, tags: ["JAVA", "OS"]},
{id:4, tags: ["PY", "NW"]}
]
Volunteer[
{id: "1", tags:["PY","NW"], name: "A"},
{id: "2", tags:["AI"], name: "B"},
{id: "3", tags:["JAVA","NW"], name: "C"},
{id: "4", tags:["JAVA","NW"], name: "D"}
]
Assign question to volunteers such that each question is assigned to at most one volunteer based on tags match.
One volunteer can take at most one question and maximise the question assigned to volunteer.
My code
class Solution {
public static class Volunteer {
private int id;
private String name;
private Set<String> tags;
public Volunteer(int id, String name, Set<String> tags) {
this.id = id;
this.name = name;
this.tags = tags;
}
}
public static class Question {
private int id;
private Set<String> tags;
public Question(int id, Set<String> tags) {
this.id = id;
this.tags = tags;
}
}
private boolean isTagMatching(Question question, Volunteer volunteer) {
for (String tag : question.tags) {
if (volunteer.tags.contains(tag)) {
return true;
}
}
return false;
}
private boolean f(int question, Question[] questions, Volunteer[] volunteers, int[] visited, int[] connections) {
for (int volunteer = 0; volunteer < volunteers.length; volunteer++) {
if (isTagMatching(questions[question], volunteers[volunteer]) && visited[volunteer] == 0) {
visited[volunteer] = 1;
if (connections[volunteer] == -1
|| f(connections[volunteer], questions, volunteers, visited, connections)) {
connections[volunteer] = question;
return true;
}
}
}
return false;
}
public void assignQuestion(Question[] questions, Volunteer[] volunteers) {
int row = questions.length;
int col = volunteers.length;
int[] c new int[col];
Arrays.fill(connections, -1);
for (int question = 0; question < row; question++) {
int[] visited = new int[col];
f(question, questions, volunteers, visited, connections);
}
for (int i = 0; i < connections.length; i++) {
if (connections[i] != -1) {
System.out.println(volunteers[i].name + " can take question " + questions[connections[i]].id);
}
}
}
public static void main(String[] args) {
Solution algo = new Solution();
Volunteer v1 = new Volunteer(1, "A", new HashSet<String>(Arrays.asList("PY", "NW")));
Volunteer v2 = new Volunteer(2, "B", new HashSet<String>(Arrays.asList("AI")));
Volunteer v3 = new Volunteer(3, "C", new HashSet<String>(Arrays.asList("JAVA", "NW")));
Volunteer v4 = new Volunteer(4, "D", new HashSet<String>(Arrays.asList("JAVA", "NW")));
Question q1 = new Question(1, new HashSet<String>(Arrays.asList("MAC", "VSCODE")));
Question q2 = new Question(2, new HashSet<String>(Arrays.asList("PY", "AI")));
Question q3 = new Question(3, new HashSet<String>(Arrays.asList("JAVA", "OS")));
Question q4 = new Question(4, new HashSet<String>(Arrays.asList("PY", "NW")));
Volunteer[] volunteers = { v1, v2, v3, v4 };
Question[] questi { q1, q2, q3, q4 };
algo.assignQuestion(questions, volunteers);
}
}