Interview Question for Software Engineer in Tests






Comment hidden because of low score. Click to expand.
0
of 0 vote

public int returnFirstUnique(int data[]){
int n=data.length;
int unique;
for(int i=0;i<n-2;i++){
for(int j=i+1;j<n;j++){
if(data[i]!=data[j]){
unique=1;}
else{
unique=-1;
break;}
}//end the inner loop
if unique>0{return unique;}
}//end the outer loop
}

this runs O(n^2) times worst case
please correct me if i am wrong... Thanks, olivebranch!

- olivebranch March 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Not sure if there is a O(n) algo to solve this.

Otherwise, one can always sort the input and find the duplicate element in O(n log n).

- novice March 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

ok... i can give an O(nlog n) solution too...

public int findUniqueData(data[i], data[j]){
j=i+1;
if(data[i]==data[j]){
int flag[i]=1;//set a flag if it is not unique
findUniqueData(data[i], data[j]);//recursive call
i++;
}
elseif(data[i]!=data[j]&&flag[i]!=1){
return data[i];
}
}

- Olivebranch March 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the above solution is considering that the array is sorted...

- olivebranch March 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorting fails here..coz we need to find the first unique character and sorting will alter the order...n^2 is one obvious solution

- sriks March 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

small correction in the above code, the inner for loop runs until j<n-1...

thanks, olivebranch!

- olivebranch March 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

List of  character is known (fixed in number ). iterate over string and find the frequency of character if it is 1 break the loop     
time complexity O(n)

- Anonymous March 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can u elaborate?

- Anonymous March 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Iterate over each character and put them in a hash table. The character is the key the position is the value. If the character is already in the hash table change the value to -1 (or whatever). Then look at the key that has the smallest positive value and you found the character.

- Microsoft dude March 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#define NUM_CHARS 256
char findUnique(char *searchString) {
  int charCounter[NUM_CHARS];
  for(int i=0; i<NUM_CHARS; i++)
    charCounter[i] = 0;
  for(i=0; searchString[i]; i++)
    charCounter[searchString[i]]++;
  for(i=0; searchString[i]; i++)
    if(charCounter[searchString[i]] == 1)
      break;
  return searchString[i];
}

- Anon March 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Anon, In your case how did we know the searchString[i] is first unique character?

eg) "dude" is input string. this case your algorithm return "e". I think that is not a question. We supposed to return "U". Is it the expection.

- N.M March 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Anon, you use extra [NUM_CHARS] space.

- someone March 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int unique(string s){
	int  a=0,b=0,x;
for(int i=0;i<(int)s.length();++i){
x=s[i]-'a'; 
            if(a&(1<<x)) b|=1<<x;
            else a|=1<<x;
}
x=(a&b);
for(int i=0;i<(int)s.length();++i) { if(!(a&(1<<(s[i]-'a'))) ) return i; }
return -1;
}

this works if the string has 'a'-'z'.

- ktr March 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Doesn't seem to be working. Could you explain the logic please?

- Anonymous March 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

im sorry , if(!(a&(1<<(s[i]-'a'))) ) return i; shud be changed to
if(!(x&(1<<(s[i]-'a'))) ) return i;

logic is actually simple, set the (char-'a')th bit in a for the first tym the char appears in the string.
if it appears second tym, set the same bit in b.
now x=a&b has the corresponding (char-'a') bits set if the char is duplicated in the string. so return the first char where (x&1<<(char-'a') fails :)

- ktr March 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your approach brings no merit. It's just an alternative of using bit vector. Your solution assumes (without justification) that string is made of [a,z] only. What about UNICODE char set, which has 2^16 chars.

- @ktr March 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i already mentioned that dude...if string has 'a'-'z' :P

- ktr March 19, 2011 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More