Google Interview Question for Software Engineers


Country: United States




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

This should be solved using Segment Tree (my thoughts).

Create a list will the distance at current snapshot and update once a person sits on a bench with max distance.

For example:
List given: 0 0 x 0 0 0 x 0 0 0
x means person sitting on the bench
0 means vacant

So, the segment tree can contain values:
2 1 0 1 2 1 0 1 2 3
distance for each index.

But I need to figure out how segment tree will be updated.

- Udbhav July 29, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Update and find next available would O(logn). Straight segment tree.

- Md Omar Faroque June 10, 2022 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Heap is also possible.

distance: list of available seats with

Always pick the max distance, pick the first available seat. If there is no seat available with that distance, drop it.

- Md Omar Faroque June 10, 2022 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This should be solved using Segment Tree (my thoughts).

Create a list will the distance at current snapshot and update once a person sits on a bench with max distance.

For example:
List given: 0 0 x 0 0 0 x 0 0 0
x means person sitting on the bench
0 means vacant

So, the segment tree can contain values:
2 1 0 1 2 1 0 1 2 3
distance for each index.

But I need to figure out how segment tree will be updated.

- Udbhav July 29, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package T13;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Stack;

public class T13 {

	private static final int seatTotal = 12;
	private static final ArrayList<Integer> result = new ArrayList<>();
	private static final Stack<Integer> midResult = new Stack<>();

	public static void main(String[] args) {

		for (int i = 0; i < seatTotal; i++) {

			if (i == 0) {
				midResult.add(i + 1);
				continue;
			} else if (i == 1) {
				midResult.add(seatTotal);
				result.addAll(midResult);
				Collections.sort(result);
				midResult.removeAll(midResult);
				System.out.println("result : " + result);
				continue;
			}
			distance(i);

		}
		System.out.println(result);
	}

	private static void distance(int index) {

		int min, max, mid = 0;
		HashMap<Integer, Integer> getH = new HashMap<>();

		if (midResult.isEmpty()) {
			min = result.get(0);
			max = result.get(1);
			mid = calc(min, max);
			if (mid == -1) {
				if (result.size() == seatTotal)
					return;
				else {
					getH = findMoreThanOne();

					Set<Integer> tempSet = getH.keySet();
					Iterator<Integer> it = tempSet.iterator();
					min = it.next();
					max = getH.get(min);
					System.out.println("====================== : min : " + min + " max : " + max);
					mid = calc(min, max);
					midResult.push(mid);

					result.addAll(midResult);
					Collections.sort(result);
					midResult.removeAll(midResult);

				}
			} else {
				midResult.push(mid);
				reset(min, max);
			}
		} else {
			min = findNext(midResult.peek());
			max = findNext(min);
			mid = calc(min, max);
			if (mid == -1)
				return;
			midResult.push(mid);
			reset(min, max);
		}

	}

	private static HashMap<Integer, Integer> findMoreThanOne() {
		HashMap<Integer, Integer> retVal = new HashMap<>();

		int min, max = 0;

		for (int i = 0; i < result.size(); i++) {
			min = result.get(i);
			for (int j = i + 1; j < result.size();) {
				max = result.get(j);

				if (calc(min, max) != -1) {
					retVal.put(min, max);
					System.out.println("findMoreThanOne : min : " + min + " max : " + max);
					return retVal;
				}

				break; // only one time processing i & i+1
			}
		}

		return retVal;
	}

	private static void reset(int min, int max) {
		Collections.sort(midResult);
		System.out.println("min : " + min + " max : " + max + " midResult : " + midResult);
		if (max == seatTotal) {
			result.addAll(midResult);
			Collections.sort(result);
			midResult.removeAll(midResult);
			System.out.println("result : " + result);
		}
	}

	private static int findNext(int min) {
		int retVal = 0;

		for (int getI : result) {
			if (getI > min) {
				retVal = getI;
				break;
			}
		}

		return retVal;
	}

	private static int calc(int min, int max) {
		int retVal = 0;
		if (((max - min) / 2) < 1)
			return -1;

		retVal = (max - min) / 2 + min;

		return retVal;
	}
}

- mannerh1 May 18, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use priority queue, this question is already covered in 2018 questions, and solution is presented.

- relaxed_coder May 20, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you please give the link to solution?

- Rajat.nitp July 02, 2020 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I ll first rephrase the question:
Given an array having 0s and 1s representing the seats. 1 means that the seat is occupied while 0 means it is vacant. Let's define the distance between 2 points

i

and

j

as

|i - j|

. We need to insert a new 1 until all the places are full. The point where 1 is inserted should be at the farthest from its nearest point. For example if the array is

000101

then next 1 will be placed at index 1 (assume 1 based indexing) since its distance from the nearest 1 is max, 3 in this case. After this the array ll be 100101, now next 1 can be inserted at any index since the distance from the nearest 1 ll always be 1. Hence we need to return [1,2,3,4]. Of course, there can be multiple answers. I assume we need to return anyone.


To solve this we make an observation, between 2 ones at i and j, the next one can be placed at a distance of abs(i - j) / 2 from either one. This is because this will be the maximum point from either of the 1s. We ll use this fact to reach the solution.

For each consecutive 1s (lets say i and j) we ll make 2 pairs -> pair<distance, index> where distance is (i + j) / 2 and index is i and j. For example for array:

110001

between one at index 2 and one at index 6 we ll make 2 pairs. The distance between the 2 is 4. So element ll be inserted at 2 distance from either. The 2 pairs ll be

<2, 1> <2, 6>. Likewise, we ll do for all consecutive ones. We ll insert all the pairs in a max heap. (the pairs ll be sorted based on the distance)

Now we know the distance each new one ll go to from each index. We ll keep popping the elemets till heap is not empty. The max element will be popped out and we ll add it's distance in the result array. But once the max distance pair is popped out, we know the entry point of the new one. Now we ll insert 2 new pairs based on the new entry. Of course, there won't be any insertions if there is no 0 between inserted one and the previous one. The cases to handle are the leftmost and the rightmost ones. We can treat those separately in the heap by making 0 and n + 1 handle their distance separately.

- Rajat.nitp July 02, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

func FarthestSeat(bench []int) int {
	s := -1
	e := -1

	n := len(bench)
	at := 0
	for i := 0; i < n; i++ {
		if bench[i] == 1 {
			if i > 0 && s == -1 && e == -1 {
				s = -i
				e = i
			} else if i-at > e-s {
				s = at
				e = i
			}
			at = i
		}
	}
	if n-at > (e-s)/2 {
		return n - 1
	}
	return (s + e) / 2
}

- BW January 02, 2021 | 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