Myntra Software Engineer Interview Experience
Round 1 - Problem Solving
Q1: Class room; number of student 1 to N.
We need to elect class leader
such that
* Every student should trust L
* L shouldn’t trust
anyone.
(x, y) => x trusts y
Input: N integer
Array pair of ints
Output: Integer = L
(1,
4) (1,2) (2, 4) (3, 4) (1,2), (3, 2)
Edge cases
(1,2), (2, 3), (1,3), (3, 2)
(4, 1), (1, 4), (2, 4) (3, 4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
TrustPairNode {
Integer key; //student
Integer value;//trust on student
}
Int getLeader(List<TrustPairNode> trustPair, int n) {
Map<Integer, Integer> leaderMap = new HashMap<>();
for (TrustPairNode node : trustPair) {
// 4 -> 3
leaderMap.put(node.value, leaderMap.getOrDefault(node.value, 0)++);
// (1, 4) (1, 2) (2, 4) (3, 4) (1,2), (3, 2) (5, 4)
If (leaderMap.containsKey(node.key)) {
leaderMap.remove(node.key);
}
}
for (Map.Entry<Integer, Integer> entrySet : leaderMap.entrySet()) {
If (entry.getValue() == n-1) {
return entry.getKey();
}
return -1;
}
|