Amazon Interview Question
SDE-2sCountry: India
Interview Type: In-Person
import java.util.LinkedList;
import java.util.List;
public class Test {
public static void main(String[] args) {
Notification notification = new Notification();
new UserNotification(notification, "ob1");
new UserNotification(notification, "ob2");
new UserNotification(notification, "ob3");
new UserNotification(notification, "ob4");
notification.setState("hello");
notification.setState("sjgs");
}
}
abstract class Observer {
Notification notification;
abstract void alert();
}
class Notification {
private String state;
private List<Observer> observers = new LinkedList<>();
void attach(Observer observer) {
this.observers.add(observer);
}
void setState(String state) {
this.state = state;
notifyObservers();
}
private void notifyObservers() {
observers.forEach(observer -> observer.alert());
}
public String getState() {
return state;
}
}
class UserNotification extends Observer {
private String user;
public UserNotification(Notification notification, String user) {
super.notification = notification;
this.user = user;
notification.attach(this);
}
@Override
void alert() {
System.out.println("User: " + this.user + " notified about " + super.notification.getState());
}
}
import java.util.Scanner;
public class EquillibriumPoint {
public static void main(String[] args) {
//code
Scanner scan = new Scanner(System.in);
int T = scan.nextInt();
for (int i = 0; i < T; i++) {
int N = scan.nextInt();
int[] A = new int[N];
for (int j = 0; j < N; j++) {
A[j] = scan.nextInt();
}
int eqlbmIndex = getEquillibriumPoint(A, N);
if (eqlbmIndex != -1)
System.out.println(eqlbmIndex + 1);
else
System.out.println(eqlbmIndex);
}
}
private static int getEquillibriumPoint(int[] A, int N) {
int eqlbmIndex = -1;
for (int i = 0; i < N; i++) {
int suml = 0;
int sumr = 0;
for (int j = 0; j < i; j++) {
suml = suml + A[j];
}
for (int k = i + 1; k < N; k++) {
sumr = sumr + A[k];
}
if (suml == sumr) {
eqlbmIndex = i;
break;
}
}
return eqlbmIndex;
}
}
- prithvish.mukherjee@techolution.com January 04, 2019