Google Interview Question
Software EngineersCountry: United States
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.
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;
}
}
Use priority queue, this question is already covered in 2018 questions, and solution is presented.
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.
This should be solved using Segment Tree (my thoughts).
- Udbhav July 29, 2019Create 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.