LinkedIn Technical Phone Screening

1084
Jun 29, 2022
Jun 29, 2022

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

  1. 0 notates false which means empty
  2. 1 notates true which means already taken
  3. Boundaries count as 0

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.

public boolean seatingAvailable(List<Boolean> seats, int peopleToSeat) {
	if(seats == null || seats.isEmpty() || peopleToSeat > seats.size()) return false;
	
	// added post interview 
	i (seats.size() == 1) {
		return seats.get(0) == false;
	}
	
	int seatsPlaced = 0;

	for(int i =0; i <seats.size(); i++) {
		if (seatsPlaced >= peopleToSeat) return true;
		
		// doesnt account for array size 1
		if(i == 0 && !seats.get(i+1)) {
			seatsPlaced ++;
			seats.set(i, true);
			continue;
		}
		
		// doesnt account for array size 1
		if(i == seats.size()-1 && !seats.get(i-1)) {
			seatsPlaced ++;
			seats.set(i, true);
			continue;
		}
		
		if (!seats.get(i-1) && !seats.get(i+1)  {
			seatsPlaced ++;
			seats.set(i, true);
		}
		
	}
	
	return seatsPlaced >= peopleToSeat;
}

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

/**
WordProcessor wp = new WordProcessor(Arrays.asList("knowing", "java", "is", "important", "for", "developing", "in", "java");
	assertEqual(2, wp.findDistgance("knowing", "important"));
	assertEqual(1, wp.findDistgance("java", "important"));
	assertEqual(1, wp.findDistgance("java", "developing"));
**/
class WordProcessor {
	public WordProcessor(List<String> words) {	
		// some code here
	}
	
	public int findDistance(String word1, String word2) {
	 // some code here
	}
}

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

  1. off by 1 for index based distance
  2. how/when to move the pointers

Map<String, List<Integer>> here where the values are just the indices.

class WordProcessor {
	Map<String, List<Integer>> wordToIndices = new HashMap()<>;
	
	public WordProcessor(List<String> words) {	
		for(int i =0; i < word.size(); i ++) {
			String word = words.get(i);
			List<Integer> indices = wordToIndices.getOrDefault(words, new ArrayList<>());
			indices.add(i);
			wordToIndices.put(word, indices);
		}
	}
	
	public int findDistance(String word1, String word2) {
		if (!wordToIndices.containsKey(word1) || !wordToIndices.containsKey(word2)) return -1;
		 
		 List<Integer> firstWordIdxs = wordToIndices.get(word1);
		 List<Integer> secondWordIdxs = wordToIndices.get(word2);
		 
		 int left = 0;
		 int right = 0;
		 int minDistance = Integer.MAX_VALUE;
		 while(left < firstWordIdxs.size() ) {
			 int idx1 = firstWordIdxs.get(left);
			 int idx2 = secondWordIdxs.get(right);
			 int distance = Math.abs(idx1 -idx2) // off by one here for 0 index I think
			
			 // move the right pointer till the distance is no longer smaller
			 if(distance > minDistance) {
			 
			 // move left pointer to search for any other shorter distances
				 while(right < secondWordIdxs.size()) {
					 int dx = firstWordIdxs.get(left);
					 int dx2 = secondWordIdxs.get(right);
					 int innerDistance = Math.abs(dx -dx2) // off by one here for 0 index I think
					 
					 if (innerDistance > distance) {
						 break;
					 }
					 
					 minDistance = Math.min(minDistance, innerDistance);
					 left ++;
				 }
				 
				
			 } else {
				 minDistance = Math.min(minDistance, distance);
				 right ++;
			 }
		 }
		 return minDistance;
	}
}

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 =/.

Comments (3)