SenseForth AI Research Private Limited.
Date : May - 2020
Mode : Hangout Video for theoretical question & CodeLite for coding
Interviewer - Ranjit Sah.
1. How do you rate yourself in java?
2. How good are you at data structure and algorithm.
4. Explain your roles and responsibility in the last projects.
5. There are couple of elements stored in doubly linked list, how to find the middle element. This doubly linked list is not the defined data structure in java collection so you can't use collection api.
6. We have a String str="lovelovexl"
Write java code to return the index of first unique character or return -1 if there is no any unique character in the given string.
Solution:
package string;
import java.util.*;
public class SenseForth {
public static void main(String[] args) {
String str="lovelovexl";
System.out.println(countChar(str));
}
static int countChar(String str){
int value=0;
LinkedHashMap<Character,Integer> hmap=new LinkedHashMap<>();
for(int i=0;i<str.length();i++){
char ch=str.charAt(i);
if(hmap.containsKey(ch)){
value=hmap.get(ch);
value++;
hmap.put(ch,value);
}else{
hmap.put(ch,1);
}
}
System.out.println(hmap);
Set set =hmap.entrySet();
Iterator it=set.iterator();
while(it.hasNext()){
Object obj=it.next();
Map.Entry entry=(Map.Entry)obj;
Integer in=(Integer)entry.getValue();
if(in==1){
char ch=(Character) entry.getKey();
return str.indexOf(ch);
}
}
return -1;
}
}
Output :
{l=3, o=2, v=2, e=2, x=1}
8
Comments
Post a Comment
Thank You for reading our post.
Your comment or feedback encourages us to improve the quality of content.
Don't forget to follow/subscribe to get the latest post.