All formula
Wednesday, 1 December 2021
Thursday, 2 September 2021
Useful questions for CSE Students
Leetcode:
- https://leetcode.com/problems/bulb-switcher-iii/
- https://leetcode.com/discuss/interview-question/406031/
Geeks:
- https://www.geeksforgeeks.org/max-count-of-unique-ratio-fraction-pairs-in-given-arrays/
Create custom Connection Pool class:
- https://www.baeldung.com/java-connection-pooling (point 4)
Graph DFS on real life scenario:
Implement the class User, representing a person in a social network, with the following functionalities:
Each user has a name. Provide a public constructor accepting that name.
Users can befriend each other with the following method:
public void befriend(User other)
Friendships are symmetric: a.befriend(b)
is equivalent to b.befriend(a).
· Clients
can check whether two users are direct friends or indirect friends (friends of
friends), using the following two methods,
public boolean
isDirectFriendOf(User other)
public boolean
isIndirectFriendOf(User other)
Solution:
| Solution |
import java.util.*;
public class User {
private String name;
private Set<User> friends = new HashSet<>();
public User(String name) {
this.name = name;
}
public void befriend(User other) {
friends.add(other);
other.friends.add(this);
}
public boolean isDirectFriendOf(User other) {
return friends.contains(other);
}
//DFS
public boolean isIndirectFriendOf(User other) {
Set<User> visited = new HashSet<>();
Stack<User> stack = new Stack<>();
stack.push(this);
while (!stack.isEmpty()) {
User user = stack.pop();
if (user.equals(other)) {
return true;
}
if (visited.add(user)) {
stack.addAll(user.friends);
}
}
return false;
}
public static void main(String...args) {
User a = new User("A"),
b = new User("B"),
c = new User("C"),
d = new User("D"),
e = new User("E");
a.befriend(b);
a.befriend(c);
d.befriend(c);
e.befriend(a);
System.out.println(b.isDirectFriendOf(c));
System.out.println(b.isIndirectFriendOf(c));
System.out.println(b.isIndirectFriendOf(d));
System.out.println(b.isIndirectFriendOf(e));
}
}


