LinkedIn | SWE | Telephone Screen
Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2020-11-09Added By Anonymous
Source: Link
2 questions. 45 mins.
1.Given two arrays(of different lengths),int lower,int upper, count how many pairs have the following property
lower <= a[i]*a[i] +b[j]*b[j] <= higher
1.Rearrange string containing only 'W','D','L' such that it always has this order till any of the characters exhaust
eg: S="WLDLLD"
o/p: "WDLDLL"
1
2 public String rearrange(String S){
3 HashMap<Character,Integer> seen=new HashMap<>();
4 StringBuilder sb=new StringBuilder();
5 char[] ch=S.toCharArray();
6
7 for(int i=0;i<ch.length;i++){
8 if(!seen.containsKey(ch[i]))
9 seen.put(ch[i],1);
10 else
11 seen.put(ch[i],seen.get(ch[i])+1);
12 }
13
14 // for(int i=0;i<S.length();i++){
15 int w=seen.getOrDefault('W',0);
16 int d=seen.getOrDefault('D',0);
17 int l=seen.getOrDefault('L',0);
18 int total=d+l+w,i=0;
19
20 while(i < total){
21 if(w > 0){
22 sb.append('W');
23 w--;
24 i++;
25 }
26 if(d > 0){
27 sb.append('D');
28 d--;
29 i++;
30
31 }
32 if(l > 0){
33 sb.append('L');
34 l--;
35 i++;
36
37 }
38
39 // i++;
40 // sb.append('W');
41
42 //}
43 }
44
45 return sb.toString();
46 }Please let me know your thoughts.
Awaiting results