LinkedIn Technical Phone Screening
Question 1: Given a List of booleans, where the list represents a row in a movie theater determine whether the input peopleToSeat can fit into the row.
Constraint you can only place someone in a seat where the two adjacent seats are empty.
Example:
input: [0,1,0,0,0,1,0,0,0,1,0,0] peopleToSeat=3
The first question I solved basically in linear time by scanning the array and checking neighbors.
I made the mistake of trying to count available empty spots first, but I even suggested that assumption but the interviewers didn't refute it till later.
I added special cases for first and last index.
Also it was super confusing because they gave examples using 0's and 1's which I know can be notation for true/false. But given that they are handing you a Boolean list I dont understand why they didn't just use true/false
Also had to clarify whether 0 was true or false and whether it mapped to empty or taken.
Notes I wrote while talking
I noted the edge cases and that my code doesn't handle the i + 1 or i-1 being out of bounds.
My original implementation was just counting open spaces without removing possibilites, so had to go through and fix that.
Also was asked to do a dry run through an example (probably added 5-10 minutes on this question).
Also last minor complaint, the IDE for coderpad sucks and kept missing curly braces and indentation.
1public boolean seatingAvailable(List<Boolean> seats, int peopleToSeat) {
2 if(seats == null || seats.isEmpty() || peopleToSeat > seats.size()) return false;
3
4 // added post interview
5 i (seats.size() == 1) {
6 return seats.get(0) == false;
7 }
8
9 int seatsPlaced = 0;
10
11 for(int i =0; i <seats.size(); i++) {
12 if (seatsPlaced >= peopleToSeat) return true;
13
14 // doesnt account for array size 1
15 if(i == 0 && !seats.get(i+1)) {
16 seatsPlaced ++;
17 seats.set(i, true);
18 continue;
19 }
20
21 // doesnt account for array size 1
22 if(i == seats.size()-1 && !seats.get(i-1)) {
23 seatsPlaced ++;
24 seats.set(i, true);
25 continue;
26 }
27
28 if (!seats.get(i-1) && !seats.get(i+1) {
29 seatsPlaced ++;
30 seats.set(i, true);
31 }
32
33 }
34
35 return seatsPlaced >= peopleToSeat;
36}Question 2: Given a List of words from a piece of text, find the minimum distance between two words.
You are given a word Processing class as such
1/**
2WordProcessor wp = new WordProcessor(Arrays.asList("knowing", "java", "is", "important", "for", "developing", "in", "java");
3 assertEqual(2, wp.findDistgance("knowing", "important"));
4 assertEqual(1, wp.findDistgance("java", "important"));
5 assertEqual(1, wp.findDistgance("java", "developing"));
6**/
7class WordProcessor {
8 public WordProcessor(List<String> words) {
9 // some code here
10 }
11
12 public int findDistance(String word1, String word2) {
13 // some code here
14 }
15}Only had about 15 minutes left due to technical issues in the first 5 minutes and dry run from previous.
Originally suggested using a HashMap and maybe two pointers to not repeat all options since I noticed there were duplicates allowed in the original list.
Didn't finish the full implementation or the two cases I mentioned
Map<String, List<Integer>> here where the values are just the indices.
1class WordProcessor {
2 Map<String, List<Integer>> wordToIndices = new HashMap()<>;
3
4 public WordProcessor(List<String> words) {
5 for(int i =0; i < word.size(); i ++) {
6 String word = words.get(i);
7 List<Integer> indices = wordToIndices.getOrDefault(words, new ArrayList<>());
8 indices.add(i);
9 wordToIndices.put(word, indices);
10 }
11 }
12
13 public int findDistance(String word1, String word2) {
14 if (!wordToIndices.containsKey(word1) || !wordToIndices.containsKey(word2)) return -1;
15
16 List<Integer> firstWordIdxs = wordToIndices.get(word1);
17 List<Integer> secondWordIdxs = wordToIndices.get(word2);
18
19 int left = 0;
20 int right = 0;
21 int minDistance = Integer.MAX_VALUE;
22 while(left < firstWordIdxs.size() ) {
23 int idx1 = firstWordIdxs.get(left);
24 int idx2 = secondWordIdxs.get(right);
25 int distance = Math.abs(idx1 -idx2) // off by one here for 0 index I think
26
27 // move the right pointer till the distance is no longer smaller
28 if(distance > minDistance) {
29
30 // move left pointer to search for any other shorter distances
31 while(right < secondWordIdxs.size()) {
32 int dx = firstWordIdxs.get(left);
33 int dx2 = secondWordIdxs.get(right);
34 int innerDistance = Math.abs(dx -dx2) // off by one here for 0 index I think
35
36 if (innerDistance > distance) {
37 break;
38 }
39
40 minDistance = Math.min(minDistance, innerDistance);
41 left ++;
42 }
43
44
45 } else {
46 minDistance = Math.min(minDistance, distance);
47 right ++;
48 }
49 }
50 return minDistance;
51 }
52}Got cut off a little early here for the 'so you can ask questions' time.
Overall very kind interviewers, a little quiet maybe because my internet connection was spotty from home =/.