Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




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

i=0;
while(i<=n/2)
{
if(a[i]==a[n-i])
i++;
else
return 0;
}
if(i>n/2)
return 1;

- Siva Krishna May 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It Will Work.
But constraint is:-Better than O(n).

- Paital May 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it's n/2 in worst case when its palindrom... so it is better than O(n)

- alexandru.doro May 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

You don't understand what O(n) means, do you...

- Anonymous May 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

well.. i know O(n)=O(n* 1/2 ), cause 1/2 is a constant.. but its significantly better that n iterations.. and i dont think you could do it in O(1)

- Anonymous May 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

seems not working...how abt string like 123456657?

- vanny May 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the question is flawed. Here is my logic.
1. If you need to find whether the string is a palindrome or not (and not guess it), you need to touch every character in the string to give the correct answer.
2. By touching every char in the string, the time complexity has to be order n where n is the length of the string.
3. it does not matter if you half the loops and touch two chars at a time. In the above comment regarding O(n*1/2), you are still touching the whole string and not half of it.

So finding whether a string is a palindrome in less than O(n) is a flawed question.

- me June 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

1. Maintain the current possible pivot index (for either odd or even count of current elements) - @pidx
2. With each new coming char (@ch) at index @i and as long as a pivot index is set (@pidx != -1), check
to see if it can be an element in a palindromic sequence up to now.
Check is done using the idea of whether the current element is the same
as the one equaly distant as the current one is from the pivot index.
If the two elements are the same and the first element is at index 0, then we
have a palindrome with this current char.

If the current element does not contribute to a palindromic sequence, then
reset the previous @pidx (if any set) and check whether this one can be a
new pivot index.

The time complexity of the above algorithm is O(1) sinece we compare the current
char only with its potential match coming before it.

- ashot madatyan May 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Not true in step of new pivot. Anyone between old pivot and new char may be the new pivot. Eg. BaaaaaB

- cacofish May 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Interesting solution. May I know how to find the current possible index? What is the logic behind choosing possible pivot?

- Manu May 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void add_char(char c) {
    // pointers to keep track of odd and even-length palindromes
    static int odd = 0, even = -1;
    static int idx = 0;     // current position in the stream
    static char stream[256];
    static bool same_char = true;
    stream[idx] = c;
    stream[idx+1] = '\0';

    if(same_char && idx > 0 && c != stream[idx-1]) {
        same_char = false;
    }
    if(same_char) 
        printf("palindrome: %s\n", stream);
    
    if(odd >= 0 && stream[odd] == c) {
        odd--;
        if(odd < 0) {
            printf("odd palindrome: %s\n", stream);
            odd = idx - 1;
        }
    } else
        odd = idx - 1;
    if(even >= 0 && stream[even] == c) {
        even--;
        if(even < 0) {
            printf("even palindrome: %s\n", stream);
            even = idx;
        }
    } else
        even = idx;
    idx++;
}

int main() {
    const char *s = "aaaaaabaaaaaa"; 
    for(int i = 0; s[i] != 0; i++)
        add_char(s[i]);
    return 0;
}

- 111 May 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i really failed to understand your logic could you please expline it throgh an algo or an example

- krishna May 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

logic is quite simple: in any given moment of time stream[cmp_pos] must match with the next incoming character stream[idx] to grow a current palindrome sequence. When 'cmp_pos' descends below 0, this indicates that we found a palindrome seq.

if stream[cmp_pos] does not match with stream[idx], we reset
cmp_pos either to 'idx' or 'idx-1' (ie the index of the last incoming character)

- 111 May 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What will happen when the stream is something like aaaaaa

- algogeek May 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@algogeek: you are right, for that matter I use a flag 'same_char' which remains 'true' until we get the first non-repeating character from the stream. please see the modified code

- 111 May 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is not correct. I think this is a fundamentally flawed approach and minor code tweaks aren't going to fix it. Consider a test case like this: bcacacb. What will your algorithm do here? If I understand correctly, your algo would first start matching things on bcac. It will then expect b to complete the palindrome bcacb. When it instead gets a, it will start matching from the back again. So then there's no way it'll see that the next two characters complete the palindrome. The problem is that the algorithm was focusing on checking one possible palindrome when the second part of the real palindrome was already forming.
_
I myself cannot find an O(1) time and O(1) extra space approach that is deterministically correct, and I actually doubt that one exists. I've come up with an O(1) space, O(1) time method involving rolling hashes, but this may not be strictly speaking guaranteed to work. With O(logN) time and space I could devise a probabilistic method that guarantees correctness and the time and space bounds in the expectation (but not not determninistically). I think with just O(1) extra space we're definitely pretty crippled here. However, O(logN) space is very small. If I want, say, 10logN bytes, if you give me a constant buffer of, say, 4KB, I can handle inputs larger than the number of atoms in the universe.

- eugene.yarovoi May 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Well of course no such inputs would be able to be stored or processed on any machine, I mean from the algorithmic perspective...

- eugene.yarovoi May 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

huh you are right eugene, there is inherent problem with this approach when palindromes are multiply included in one another.. seems that this is not a trivial problem as it stands ))

- 111 May 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

always has the mid of the stream.
Now check going left and right

can be done in O(n/2)

- DashDash May 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

is still O(n). O(kn) is still O(n).

- ani May 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

in that case you can have mid of stream and calculate hash of both the halfs. It should be same

- DashDash October 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use a variant of rabin karp algorithm.

Each character update is O(1).
maintain a hash for the first and second half
When a new (odd) character is added only the second part moves just like in every pattern matching algorithm.

When a new (even) character is added
a. the first and second part are added a character.
O(1) per update.

O(n) average time for entire sequence.

- Liran May 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use rabin karp for either side of mid point. keep shifting mid point and recalculating rabin karps.

O(1) time O(1) space.

- pkn May 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use rabin karp for either side of mid point. keep shifting mid point and recalculating rabin karps.

O(1) time O(1) space.

- pkn May 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think this is not possible in space of O(1) and time O(1). If so, there must be a regular expression for defining a palindrome to build a finite automaton - there is no - therefore it is not possible to detect infinite long sequences with finte space.

- Stefan May 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean isPalendrome(char[] charlist) {
		for (int i=0; i<charlist.length/2; i++) {
			if (charlist[i] != charlist[charlist.length-i-1]) return false;
		}
		
		return true;

}

- Fasih May 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1)Maintain two hashes, one for upper half and other for lower half.
2)Maintain the count of characters.
3)When a new element is added, check the new count.
4)Wen the count is two,both the halfs will be having hash value of one characted each.
5)When a third element is added, check the count.
6)Since the count is odd,remove the hash value of first character in 2nd hash and make that character middle element.
7)Add the hash value of newly entered character to 2nd hash.
8)When a fourth element is added,add the hash value of middle character to first hash and add the hash value of newly entered character to 2nd hash.

Please let me know if you find any flaw in this approach.

Thanks

- Mishe June 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

mantaining hash will require extraa memory space and it is mentioned no extraa space to be used

- varun July 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

For i = 1 to n
A[0] = A[i] xor A[0]
return A[0]

- Anonymous May 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Its wrong.It will return TRUE for anagrams too.

- Anonymous May 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Its wrong.It will return TRUE for anagrams too.

- code_guru May 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Its wrong.It will return TRUE for anagrams too.

- code_guru May 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Still the complexity remains o(n).

- Shiva May 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It will be O(N^2), not n/2.
Check palindrome for every character arrival.

- paital May 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It will be O(N^2), not n/2.
Check palindrome for every character arrival.

- paital May 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It will be O(N^2), not n/2.
Check palindrome for every character arrival.

- paital May 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It will be O(N^2), not n/2.
Check palindrome for every character arrival.

- paital May 22, 2012 | 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