Amazon Interview Question for SDE-2s


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.sample.test;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class NotificationSystem {

	public static void main(String[] args) {
		ServiceProvider sp = new ServiceProvider();
		Subscriber sub = new Subscriber();
		Scanner ss = new Scanner(System.in);
		System.out.println("List of available services with their Ids ::");
		sp.getSubscriptions().stream().forEach(System.out::println);
		System.out.println("Please select Ids that you wish to subscribe to separated by ( , ) ::");
		String[] strIds = ss.nextLine().split(",");
		int[] ids = Stream.of(strIds).mapToInt(s -> Integer.valueOf(s.trim())).toArray();
		sp.registerForService(sub, ids);
		sp.getSubscriptionMap().entrySet().stream().map(e -> {
			return String.format("Subscriber Subscribed to %s", e.getKey());
		}).forEach(System.out::println);

		Random r = new Random();
		int[] stop = new int[] { -1 };
		while (stop[0] != 3) {
			sp.getSubscriptions().stream().forEach(subscription -> {
				if (stop[0] != 3) {
					int upper = 10000;
					int lower = 2000;
					// so that we have lower >> num >> upper
					subscription.setData(r.nextInt(upper - lower) + lower);
					try {
						Thread.sleep(1000);
					} catch (Exception e1) {
						System.out.println("Error " + e1);
					}

					stop[0] = r.nextInt(10);
					if(stop[0] == 3) {
						System.out.println(".... Stopping");
					}
				}
			});
		}

	}

}

class Subscriber {
	ServiceProvider pub;

	void getNotified(Subscription s) {
		System.out.println(s);
		// System.out.println(s.msg);
	}

	public Set<Subscription> getSubscList() {
		return pub.getSubscriptions();
	}

	public void subscribe(int... serviceIds) {
		pub.registerForService(this, serviceIds);
	}

}

class ServiceProvider {

	private Map<Subscription, List<Subscriber>> subscriptionMap = new LinkedHashMap<Subscription, List<Subscriber>>();
	private Set<Subscription> subscriptions = new LinkedHashSet<Subscription>();

	Subscription s1 = new Subscription(1, 1000);
	Subscription s2 = new Subscription(2, 2000);
	Subscription s3 = new Subscription(3, 3000);
	Subscription s4 = new Subscription(4, 4000);
	Subscription s5 = new Subscription(5, 5000);

	public ServiceProvider() {
		addSubscription(s1, s5, s2, s4);
	}

	public Map<Subscription, List<Subscriber>> getSubscriptionMap() {
		return subscriptionMap;
	}

	public Set<Subscription> getSubscriptions() {
		return subscriptions;
	}

	void addSubscription(Subscription... subs) {
		Stream.of(subs).forEach(s -> {
			if (!subscriptions.contains(s)) {
				s.setPublisher(this);
				subscriptions.add(s);
			}
		});
	}

	void registerForService(Subscriber subscriber, int... serviceIds) {
		IntStream.of(serviceIds).forEach(serviceId -> {
			Set<Subscription> sFound = subscriptions.stream().filter(s -> s.id == serviceId)
					.collect(Collectors.toCollection(LinkedHashSet<Subscription>::new));
			if (null != sFound && sFound.size() > 0) {
				Subscription s = sFound.iterator().next();
				if (subscriptionMap.containsKey(s)) {
					subscriptionMap.get(s).add(subscriber);
				} else {
					subscriptionMap.put(s, Arrays.asList(subscriber));
				}
			} else {
				System.err.println("No service available with serviceId :: " + serviceId);
			}
		});
	}

	void notifyConsumers(Subscription... subs) {
		Stream.of(subs).forEach(s -> {
			if (subscriptionMap.containsKey(s)) {
				List<Subscriber> consumers = subscriptionMap.get(s);
				if (null != consumers) {
					consumers.stream().forEach(con -> con.getNotified(s));
				}
			}
		});
	}
}

class Subscription {
	final int id;
	int data;
	String msg = "";
	ServiceProvider serviceProvider;

	public Subscription(int id, int data) {
		this.id = id;
		this.data = data;
	}

	public Subscription(int id) {
		this.id = id;
	}

	public void setPublisher(ServiceProvider serviceProvider) {
		this.serviceProvider = serviceProvider;
	}

	public void setData(int new_data) {
		if (new_data != data) {
			msg = String.format("Subscription data changed from %d to %d", data, new_data);
			data = new_data;
			serviceProvider.notifyConsumers(this);
		}
	}

	@Override
	public boolean equals(Object obj) {
		if (null == obj) {
			return false;
		}

		if (obj instanceof Subscription) {
			Subscription o2 = (Subscription) obj;
			return o2.id == this.id;
		}

		return false;
	}

	@Override
	public String toString() {
		return "Subscription [id=" + id + ", data=" + data + ", msg=" + msg + "]";
	}

}

- prithvish.mukherjee@techolution.com January 04, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

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;
 }
}

- Anonymous August 15, 2018 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

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.

Learn More

Resume Review

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.

Learn More

Mock Interviews

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.

Learn More