Amazon Interview Question for SDE1s


Country: United States




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

@Venkat: no it isn't linear it's O(n*m) as it's implemented not efficient:
first string: "1111111111111"
pattern: "113"
the way it is imlemented
tries at index 0: 1 match, 1 match, 1,3 no match,
tries at index 1: 1 match, 1,match, 1,3 no match
etc.
roughly 3*n iterations instead of 13, if it was linear, the pattern couldn't influence that, so let's try pattern "13" ... roughly 2*n iterations

it's definitely not linear as it seldom is with nested for-loops ;-)

- Chris May 10, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 3 vote

str1.indexof('c'): O(str1.length)

String str2 = "searchpattern"
str1.indexof(str2): O(str1.length * str2.length); // sadly, I checked the implementation, no rabin karp etc... :-(

- Chris May 08, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

@majia168: how comes you have so many questions?

- Chris May 08, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Returns the index within this string of the first occurrence of the specified substring/character, the first occurance of character can be on any postition of the original string.
So the Time complexity would be Linear.

- Venkat Peddi May 10, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A strange question, actually.

The default implementation of indexOf(String) is O(len1 x len2), however...
There are intrinsics for all major platforms. These implementations are far more effective, ~O(len1 + len2).

So, I wonder what the interviewer wanted to hear...

- petrov4feedly May 22, 2017 | Flag Reply


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