Amazon Interview Question for Software Engineer / Developers






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

Using recursion we can check linked list is palindrome.
#include "stdafx.h"
#include <iostream>
using namespace std;
int ispalindrome(node*, int[], int&, int, int&);
struct node
{
	int data;
	struct node *link;
};
int _tmain(int argc, _TCHAR* argv[])
{
	struct node *head=NULL, *ss=NULL;
	
	insert(&head, 1);
	insert(&head, 2);
	insert(&head, 3);
	insert(&head, 3);
	insert(&head, 2);
	insert(&head, 3);
	insert(&head, 1);

	int a[100];
	int k=0, l=1;
	cout << "Is palindrome: " << ispalindrome(head1, a, k, 0, l) << endl;
}
int ispalindrome(node *head, int a[], int &i, int n, int &flag)
{
	if(head == NULL)
		return 0;
	a[n++] = head->data;
	ispalindrome(head->link, a, i, n, flag);
	if(a[i++] != head->data)
		flag = 0;
	return flag;
}

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

Ya....good trick. you are using recursion for storing additional data.

- GekkoGordan February 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice solution

- siva.sai.2020 March 08, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Reverse the second half and then check each node:
1>2>3>4>5>4>3>2>1 => 1>2>3>4>5<4<3<2<1

- S February 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

O(n) time and O(1) space - reverse without recursion

- S February 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Great answer.

- siva.sai.2020 February 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Both above are good and have same logic. 1st one if list is not editable, 2nd if editing allowed.

- Anurag Singh February 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nope buddy, thats wrong.
As @gekko says the recursion algo is using an additional array. In the example it uses "int a[100];" and if the node contains some object other than 'int' then this approach would need an array of objects. which is basically worse than iteration.
Instead of the recursion you could simply copy the linked list to an array and check for palindrome iteratively which has same time complexity and lesser space complexity than recursion algo above.

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

@S: To find whether a Linked list is palindrome, you are
modifying the list, this is not good. If you copy to
another list and check means you are using another data
structure, that is not allowed.

@Anonymous: Copying the list to array is also not allowed.
Because array is also a data structure (Question without
using any data structure)

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

It's pretty easy to restore the list afterwards, by just reversing the second half again? It's still O(n)

- Anon February 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, reversing it back seems to be okay. Not sure if there is a better way.

- Anonymous February 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

reposting the above code without using arr[100], as this is also a data structure, as per the question it is not allowed to use.
#include "stdafx.h"
#include <iostream>
using namespace std;
int ispalindrome(node*, node**, int&, int, int&);
struct node
{
	int data;
	struct node *link;
};
int _tmain(int argc, _TCHAR* argv[])
{
	struct node *head=NULL, *h1;
        h1 = head;
	
	insert(&head, 1);
	insert(&head, 2);
	insert(&head, 3);
	insert(&head, 3);
	insert(&head, 2);
	insert(&head, 3);
	insert(&head, 1);


	int k=1;
	cout << "Is palindrome: " << ispalindrome(head1, &h1, k) << endl;
}
int ispalindrome(node *head, node**h1, int &flag)
{
        int b;
	if(head == NULL)
		return 0;
	b = head->data;
	ispalindrome(head->link, a, i, n, flag);
	if(b != (*h1)->data)
		flag = 0;
        (*h1) = (*h1)->link;
	return flag;
}
Still here using 2 int variables and 1 node* variable.

- Anonymous February 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Finally got O(n) time and O(1) space solution:
#include "stdafx.h"
#include <iostream>
using namespace std;
void ispalindrome(node*, node**);
struct node
{
	int data;
	struct node *link;
};
int _tmain(int argc, _TCHAR* argv[])
{
	struct node *head=NULL, *h1;
        h1 = head;
	
	insert(&head, 1);
	insert(&head, 2);
	insert(&head, 3);
	insert(&head, 3);
	insert(&head, 2);
	insert(&head, 3);
	insert(&head, 1);

        int k=1;
	ispalindrome(head1, &h1);
	if(h1 == NULL)
		cout << "YES";
	else
		cout << "NO";
}
void ispalindrome(node *head, node**h1)
{
	if(head == NULL)
		return 0;
	ispalindrome(head->link, h1);
	if(*h1)
	{
		if(head->data != (*h1)->data)
			return;
		(*h1) = (*h1)->link;
	}
	return;
}

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

couple of typos are there:

1. int k=1 is not required
2. if(head == NULL)
return; // not return 0;

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

if() condition is not required.
void ispalindrome(node *head, node**h1)
{
	if(head == NULL)
		return 0;
	ispalindrome(head->link, h1);
	if(head->data != (*h1)->data)
		return;
	(*h1) = (*h1)->link;
	return;
}

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

If you unroll the recursion what's happening is the algo is creating a 'n' additional pointers by passing head->link.
so basically all the algo is doing is constructing an array of additional pointers to each element and is visiting them in the reverse order and simultaneously comparing by to element at 'h1' in the forward order.
i.e. using an additional space of O(n) {array of n pointers}
Also a small suggestion on the terminology, people usually refer to next element as "head->next" rather than "head->link"
thanks

- chennavarri February 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

why should we need a double pointer here. We should be good with single pointer also.

- maan February 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@chennavarri: You are correct it will take O(n) space for pointers. If we can destroy the linked list then we can pass by reference so that it will not create copies for every recursive call.

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

I believe internally the compiler would still be passing n references for the complete recursion cycle. anyways, the solution by S is the best one I think.

- chennavarri February 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

why should we need a double pointer here. We should be good with single pointer also.

- maan February 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If this is single pointer means passing by value, it will create a copy every time if passes to ispalindrome(). If this is double pointer means passing by reference and it will not create copies for each recursive call.

- Anonymous February 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int is_palindrome(node *n1) {

static node *n2 = start;
static node *n_end = NULL;

if (n2->next == n_end)
return 2;
else if (n1->next == n_end) {
n_end = n1;
if (n2->data == n1->data) {
n2 = n2->next;
return 1;
}
else
return 0;
}
else {
do {
rc = is_palindrome(n1->next);
n1 = n2;
} while (rc == 1);
return rc;
}

}

If rc == 2 then link list is palindrome.

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

you cant use arrays.
who will take care if linked list elemnts are more than array max?

- prashant July 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

also, no production code uses recursion.

- prashant July 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

NOTE: I am learning efficient programming(i.e.. time & space optimized code,easily understandable code).If you find any error in my below code, could you please let me know errors/suggestions in the code.

struct list
{
	int data;
	struct list *next;
};


int isPalindromeRecursive(struct list *header, struct list **traverseNode)
{
	if( (*traverseNode)->next == NULL )
	{		
		*traverseNode = header->next;		
		return 1;
	}
	else if((*traverseNode)->next->next == NULL)
	{		
		*traverseNode = header->next->next;
		return header->data == header->next->data; // when list contains even no. of nodes
	}
	else
	{
		*traverseNode = (*traverseNode)->next->next;
		if(!isPalindromeRecursive(header->next, traverseNode))
			return 0;
		if(header->data != (*traverseNode)->data)
		{			
			return 0;
		}
		else
		{
			*traverseNode = (*traverseNode)->next;
			return 1;
		}
	}
}

int isPalindrome(struct list *header)
{
	if( header == NULL )
	{
		printf("\n Invalid list \n");
		return -1; 
	}
	else if(header->next == NULL)
	{
		return 1;
	}
	else if( header->next->next == NULL )
	{
		return header->data == header->next->data;
	}
	else
	{
		struct list *traverseNode = header;
		if(isPalindromeRecursive(header, &traverseNode))
		{
			printf("\n list is Palindrome \n");
return 1;
		}
		else
		{
			printf("\n list is not Palindrome \n");
return 0;
		}
	}
}

- siva.sai.2020 February 15, 2013 | 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