is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.
CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.
Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.
Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.
class DisjointSet {
- 4shahidhussain April 16, 2019private final int[] parents;
DisjointSet(int size) {
this.parents = new int[size];
// Initially, there are `size` separate items having themselves as parents.
for (int i = 0; i < size; i++) {
this.parents[i] = i;
}
}
void connect(int a, int b) {
parents[getParent(b)] = getParent(a);
}
int getParent(int a) {
int parent = parents[a];
if (parent != a) {
parent = getParent(parent);
parents[a] = parent;
}
return parent;
}
}
static String next(String s, List<int[]> swaps) {
final int N = s.length();
// 1a. Extract separate graphs.
DisjointSet set = new DisjointSet(N);
for (int[] swap : swaps) {
set.connect(swap[0], swap[1]);
}
// 1b. Map the string’s characters to the appropriate vertices.
Map<Integer, List<Character>> graphs = new HashMap<>();
for (int i = 0; i < N; i++) {
int head = set.getParent(i);
List<Character> characters = graphs.computeIfAbsent(head, (dummy) -> new ArrayList<>());
characters.add(s.charAt(i));
}
// 2. Within each graph, sort the characters in **ascending** order.
for (List<Character> characters : graphs.values()) {
characters.sort(null);
}
// 3. Populate the output by taking the characters from the graph.
StringBuilder sb = new StringBuilder(N);
for (int i = 0; i < N; i++) {
// Since the lists are sorted in ascending order, take the last element.
// This is similar to pop_back() function of std::vector.
List<Character> characters = graphs.get(set.getParent(i));
char currentMax = characters.remove(characters.size() - 1);
sb.append(currentMax);
}
return sb.toString();
}
static String next(String s, List<int[]> swaps) {
final int N = s.length();
// 1a. Extract separate graphs.
DisjointSet set = new DisjointSet(N);
for (int[] swap : swaps) {
set.connect(swap[0], swap[1]);
}
Function<Integer, Queue<Character>> newDescendingQueue = (dummy) -> new PriorityQueue<>(
Comparator.reverseOrder());
// 1b. Map the string’s characters to the appropriate vertices.
Map<Integer, Queue<Character>> graphs = new HashMap<>();
for (int i = 0; i < N; i++) {
int head = set.getParent(i);
graphs.computeIfAbsent(head, newDescendingQueue).offer(s.charAt(i));
}
// 2. PriorityQueue guarantees the descending order when polling elements.
// 3. Populate the output by taking the characters from the graph.
StringBuilder sb = new StringBuilder(N);
for (int i = 0; i < N; i++) {
sb.append(graphs.get(set.getParent(i)).poll());
}
return sb.toString();
}