Linkedin senior SDE Android interview - Phone screen (California -Sunnyvale)
Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2021-08-17Added By Anonymous
Source: Link
I had 45 phone interview round, I was asked very breif intro and two DS questions -
Interviewer was super nice.
I was able to complete both questions and now on the Virtual Onsite stage.
/**
DS question 1 ::
•This function determines if the braces '(' and ')' in a string are properly matched.
•it ignores non-brace characters.
•Some examples:
•"()()()()" -> true
•"((45+)*a3)" -> true
•"(((())())" -> false )))(((
•"(())))))))"
•*/
1public boolean matched(String s) {
2 // Implementation here
3 int braces = 0;
4
5 // O(N) Time
6 // O(1) space
7 for(int i=0;i<s.length();i++) {
8 char c = s.charAt(i);
9
10 // if(stack.isEmpty()) []
11 switch(c){
12 case '(':
13 braces++; // 4 -2 + 1 -2
14 break;
15 case ')':
16 braces--;
17 if(braces<0) {return false;}
18 break;
19 default:
20 // do nothing
21 break;
22 }
23
24 return braces ==0;
25 }
26
27}/*
DS Question 2:
This class will be given a list of words (such as might be tokenized
•from a paragraph of text), and will provide a method that takes two
•words and returns the shortest distance (in words) between those two
•words in the provided text.
•Example:
•SentenceDistanceFinder finder = new SentenceDistanceFinder(Arrays.asList("the", "quick", "brown", "foxi", "quick"));
•assert(finder.distance("fox", "the") == 3);
•assert(finder.distance("quick", "fox") == 1);
•
•"quick" appears twice in the input. There are two possible distance values for "quick" and "fox":
1(3 - 1) = 2 and (4 - 3) = 1.•Since we have to return the shortest distance between the two words we return 1.
•*/
1public class SentenceDistanceFinder {
2
3 Map<String, List<Integer>> wordMap;
4 public SentenceDistanceFinder(List<String> words) { // O(N) Time // Space : O(N)
5 wordMap = new HashMap<>();
6
7 for(int i=0;i<words.size();i++){
8 List<Integer> list = wordMap.getOrDefault(words.get(i), new ArrayList<Integer>);
9 list.add(i);
10 wordMap.put(words.get(i), list);
11 }
12
13 }
14
15 public int distance(String wordOne, String wordTwo) { //Time : O(N) // Space : O(N)
16
17 int min = Integer.MAX_VALUE;
18
19 List<Integer> indexes1 = null;
20 List<Integer> indexes2 = null;
21
22 if(wordMap.containsKey(wordOne)) {
23 indexes1 = wordMap.get(wordOne); // ArrayList({1,4}) >2
24 }
25
26
27 if(wordMap.containsKey(wordTwo)) {
28 indexes2 = wordMap.get(wordTwo);// ArrayList({3}) >1
29 }
30
31 if(indexes1 != null && indexes2 != null) {
32 int i1 =0;
33 int i2 =0;
34
35 while (i1<indexes1.size() && i2.indexes2.size()) {
36 min = Math.min(min, Math.abs(indexes1.get(i1)-indexes2.get(i2)));
37 if(indexes1.get(i1) < indexes2.get(i2)) {
38 i1++;
39 } else {
40 i2++;
41 }
42 }
43 }
44 return min;
45 }
46}