Goldman Sachs Interview Question for Backend Developers


Country: United States
Interview Type: In-Person




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

Is the order fixed in which you have to visit the channels or can you pick the order?
what is the starting start and end channel used for? Does it mean if you start the channel "start" was running and channel "end" should be set after visiting all channels in that array?

I assume the order is fixed and we can create an array C [start, A[0], A[1], ... ,A[n-1], end] that is the sequence I have to tune channels in.

I need seiling(log10(C[i]+1)) key hits to enter the number C[i]
I need ABS(C[i-1] - C[i]) key hits to tune in using + or -
I need ABS(C[i-2] - C[i]) + 1 key hits to tune in using back and + or -
I will have to use the sum of above min for each C[i] 0 < i < n key hits to tune in all assuming C[0] = start was tuned in when I started.

- Chris August 31, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
- anonymousNg August 31, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
		String[] cnos = { "2", "4", "600", "60", "100", "10", "50", "5", "77" };
		String start = "60";
		String end = "50";
		int stInd = -1;
		int eInd = -1;
		sort(cnos, 0, cnos.length, new HashMap<String, Double>());
		
		for (int i = 0; i < cnos.length; i++) {
			if (cnos[i].equals(start)) {
				stInd = i;
			}
		}
		rotate(cnos, stInd);
		for (int i = 0; i < cnos.length; i++) {
			if (cnos[i].equals(end)) {
				eInd = i;
			}
		}
		swap(cnos, eInd, cnos.length-1);
		int[] minclicks = new int[1];
		minclicks[0] = Integer.MAX_VALUE;
		countclicks(start, end, cnos, "", cnos.length, 0, minclicks, 0, cnos.length - 1);
		System.out.println(minclicks[0]);
	}

	public static int countclicks(String start, String end, String[] cnos, String ch, int n, int clicks,
			int[] minclicks, int stInd, int len) {

		if(n < 0)
			return clicks;
		
		if (ch.equals(end) && n == 0) {
			minclicks[0] = Math.min(clicks, minclicks[0]);
			return clicks;
		}

		String last = start;
		for (int i = stInd; i <= len; i++) {
			clicks = countclicks(start, end, cnos, cnos[i], n-1, clicks+clicks(last, cnos[i]), minclicks, i + 1, len);
			last = cnos[i];
		}
		return clicks;
	}

	public static void rotate(String[] arr, int p){
		int n = p;
		int m = arr.length;
		while(n > 0){
			int i = p;
			String t = arr[i];
			int x = m;
			while(i >= 0 && x > 0){
				if(i == 0)
					i = arr.length;
				String te = arr[i-1];
				arr[i-1] = t;
				t = te;
				i--;
				x--;
			}
			n--;
			p--;
		}
	}
	
	public static int clicks(String lastChannel, String nextChannel) {

		int lch = Integer.parseInt(lastChannel);
		int nch = Integer.parseInt(nextChannel);
		if (lch - nch == 1 || lch - nch == -1)
			return 1;

		int index = -1;
		if (lastChannel.length() <= nextChannel.length()) {
			index = nextChannel.indexOf(lastChannel);
			if (index == 0)
				return nextChannel.length() - lastChannel.length();
		} else {
			index = lastChannel.indexOf(nextChannel);
			if (index == 0)
				return lastChannel.length() - nextChannel.length();
		}

		return nextChannel.length();

	}

	public static void sort(String[] arr, int l, int h, Map<String, Double> hMap) {
		if(l >= h)
			return;
		int n = arr.length;
		String mid = arr[(h - l) / 2 + l];
		int i = 0;
		int j = n - 1;

		if (hMap.get(mid) == null)
			hMap.put(mid, hash(mid));
		double midhash = hMap.get(mid);

		while (i < j) {
			if (hMap.get(arr[i]) == null)
				hMap.put(arr[i], hash(arr[i]));
			if (hMap.get(arr[j]) == null)
				hMap.put(arr[j], hash(arr[j]));

			double iVal = hMap.get(arr[i]);
			double jVal = hMap.get(arr[j]);

			while (iVal < midhash) {
				i++;
				if (hMap.get(arr[i]) == null)
					hMap.put(arr[i], hash(arr[i]));
				iVal = hMap.get(arr[i]);
			}
			while (jVal > midhash) {
				j--;
				if (hMap.get(arr[j]) == null)
					hMap.put(arr[j], hash(arr[j]));
				jVal = hMap.get(arr[j]);
			}
			if (i <= j) {
				swap(arr, i, j);
				i++;
				j--;
			}
		}
		if (i < h)
			sort(arr, i, h, hMap);
		if (j > l)
			sort(arr, l, j, hMap);
	}

	public static double hash(String str) {
		char[] carr = str.toCharArray();
		int i = 0;

		double hash = 0;
		while (i <= carr.length - 1) {
			hash += Integer.parseInt(String.valueOf(carr[i]))+0.1;
			i++;
		}
		return hash;
	}

	public static void swap(String[] arr, int i, int j) {
		String t = arr[i];
		arr[i] = arr[j];
		arr[j] = t;
	}

- sudip.innovates October 13, 2017 | 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