Amazon.com Interview Question
1,578 Interview Reviews |
Back to all Amazon.com Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Development Engineer Intern at Amazon.com:
Return the index of the first repeated character of a string.
See more for this Amazon.com Software Development Engineer Intern Interview
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (6)
// Return the index of the first repeated character of a string.
String s = "abdcbm";
boolean[] map = new boolean[28];
for (int i = 0; i < s.length(); i++) {
if (!map[s.charAt(i) - 'a']) {
map[s.charAt(i) - 'a'] = true;
} else {
System.out.println(i);
break;
}
}
}
Helpful Answer?
Yes |
No
Inappropriate?
Helpful Answer?
Yes |
No
Inappropriate?
1 of 1 people found this helpful
void func( char const* str)
{
char val = 0;
for(int i = 0; i < strlen(str); i++)
{
mask = 1 << str[i];
if(mask & val)
return i;
else
val |= mask;
};
};
}
Helpful Answer?
Yes |
No
Inappropriate?
dict = {}
l = len(str)
for i in range(0,l):
c = str[i]
if c in dict:
return dict[c]
else:
dict[c] = i
Helpful Answer?
Yes |
No
Inappropriate?
def func(str) :
dict = {}
l = len(str)
for i in range(0,l):
c = str[i]
if c in dict:
return dict[c]
else:
dict[c] = i
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up



0 of 0 people found this helpful
by Interview Candidate:
I'm not sure if there is a way to do this without using a temporary data structure, but this is O(n) in worst case time complexity, so it's pretty good.