Amazon Interview Question for Software Engineer in Tests


Country: United States
Interview Type: Phone Interview




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

1. Set one pointer at start and another at last
2. Start pointer move next
3 last pointer move prev
4. At each move 2&3 check if both values not equal return false
Else return true outside the loop

- Dinkar February 19, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

LinkedList.metaClass.fillData { String pTest ->
    for( c in pTest.toCharArray()) {
        delegate.add(c)
    }
    return delegate
}

public boolean isPalindrome(LinkedList list){
    if(list.size()==0) return true;
    def asc = list.iterator();
    def desc = list.descendingIterator();
    while ( asc.next()==desc.next() ){
        if (!asc.hasNext()) return true;
    }
    return false
}


assert isPalindrome(new LinkedList().fillData("lol"))
assert !isPalindrome(new LinkedList().fillData("nottrue"))

- Dmitry Gorbunov May 29, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My Javascript solution, you can only iterate upto middle if you are maintaining size of list.

function node(data){
    this.data = data;
    this.next = null;
    this.previous = null;
}

function list(){
    this.head = null;
    this.tail = null;
}

list.prototype.add = function(data){
    let n =new node(data);
    if(!this.head){
        this.head = n;
        this.tail = n;
        return;
    }
    this.tail.next = n;
    n.previous = this.tail;
    this.tail = n;
    
}

let l  =  new list();
l.add('a')
l.add('b')
l.add('c')
l.add('b')
l.add('a')



function ispalindrome(l){
    let h = l.head;
    let t = l.tail;

    while(h){
        if(h.data !== t.data){
            console.log('no');
            return
        }
        h = h.next;
        t = t.previous;
    }
    console.log('yes')
}

ispalindrome(l)

- pinn.chait February 19, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java. Just keep two pointers and run front and back checking data of these pointers.

class Node {
	Node next = null;
	Node back = null;
	int data;

	public Node() {
	}

	public Node(Node next, Node prev, int data) {
	this.next = next;
	this.back = prev;
	this.data = data;
	}

}

private boolean isPalindrome(Node start) {
		if(start == null)
			return false;

		if(start.next == null)
			return true;

		Node rearRunner = start; // from middle to back till start
		Node runner = start.next;
		Node frontRunner = runner; //from middle to end.
		while(runner.next != null ) {
			runner = runner.next;
			frontRunner = frontRunner.next;
			if(runner.next != null) {
				runner = runner.next;
				rearRunner = rearRunner.next;
			}
		}
		
		boolean isPalindrome = true;
		
		while(frontRunner != null) {
			if(rearRunner.data != frontRunner.data) {
				isPalindrome = false;
				break;
			} else {
				rearRunner = rearRunner.back; 
				frontRunner = frontRunner.next;
			}
				
		}
		
		return isPalindrome;

	}

- v32 February 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class DLLPalindrome {
	static DoublyLinkedList DLL = new DoublyLinkedList();

	public static void main(String args[])
	{
		DLLPalindrome dp = new DLLPalindrome ();
		dp.populateDLL();
		DLL.iterateForward();
		dp.isPalindrome(DLL);
	}

	private void isPalindrome(DoublyLinkedList dll) {
		for(int i=0;i<dll.getSize()/2;i++)
		{
			if(dll.head.data == dll.tail.data)
			{
			dll.head  = dll.head.next;
			dll.tail  = dll.tail.prev;
			}
			else
			{
				System.out.println("not a palindrome");
			}
		}
		
	}

	private void populateDLL() {

		DLL.addDataToFirst(4);
		DLL.addDataToFirst(5);
		DLL.addDataToLast(4);
		DLL.addDataToLast(5);

	}
	
}

- TonyStark February 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Public static boolean IsPaL(Node head){
        if(head == null || head.next == null)return true;
        int size = 1;
        Node temp = head;
        while(temp.next != null){
            size++;
            temp = temp.next;
        }
        int middle;
        if(size % 2 == 1)middle = (size/2)+1;
        else{
              size = size / 2;  
         }
         Node temp1 = head;
         while(middle != 0){
             if(temp1.data != temp.data)return false;
             temp = temp.prev;
             temp1 = temp1.next
         }
         return true;

}

- ee February 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

temp1 = temp1.next;
middle--;

- Kirk April 02, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Node:
    def __init__(self,data):
        self.data = data
        self.next = None
        self.prev = None
class Linkedlist:
    def __init__(self):
        self.head = Node(None)

    def AddNode(self,data):
        newnode = Node(data)
        currnode = self.head.next
        if currnode == None:
            currnode = self.head
        while currnode.next is not None:
            currnode = currnode.next
        currnode.next = newnode
        newnode.prev = currnode

    def IsPalindrome(self):
        tempstring_frwd= ""
        tempstring_rev = ""
        if self.head.next == None :
            print "LinkedList doesn't have any nodes"
            return
        currnode = self.head.next
        while currnode.next :
            tempstring_frwd += str(currnode.data)
            currnode = currnode.next
        tempstring_frwd += str(currnode.data)
        while currnode.prev:
            tempstring_rev += str(currnode.data)
            currnode = currnode.prev
        if tempstring_rev == tempstring_frwd :
            return "Palindrome"
        else:
            return "Not Palindrome"
dl = Linkedlist()
dl.AddNode(1)
dl.AddNode(2)
dl.AddNode(3)
dl.AddNode(2)
dl.AddNode(1)
print dl.IsPalindrome()

- ankitpatel2100 February 26, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

C++ Solution

struct Node {
  
    string data;
    Node *next;
    Node *prev;
};


Node* createNode(string& A, Node *prev, Node* next);
void display(Node* head);
bool isPalindrome(Node* firstNode, Node* lastNode);


void destroyList(Node* first);

int main(int argc, char** argv)
{
    string A [] = {"A", "B","B","A"};
    
    int size = sizeof(A) / sizeof(A[0]);
    
    // Head Node
    Node * firstNode = NULL;
    Node * lastNode  = NULL;
    
    for ( int index=0; index <size  ; index++ )
    {
        if ( firstNode == NULL )
        {
            firstNode = createNode(A[index], NULL, NULL);
            lastNode  = firstNode;
        }
        else
        {
            lastNode->next = createNode(A[index], lastNode, NULL );
            lastNode = lastNode->next; // Move to the next node
        }
    }
     
   display(firstNode);
    
    // Check is plaindrome?
    cout<<"Is Palindrome:"<< isPalindrome(firstNode, lastNode) <<endl;
    
    display(firstNode);    
    return 0;
}


Node* createNode(string & A, Node* prev, Node* next)
{
    Node* newPtr = new Node();
    newPtr->data = A;
    newPtr->next = next;
    newPtr->prev = prev;
    
    return newPtr;
}

// Is Palindrome
bool isPalindrome(Node* firstNode, Node* lastNode)
{
    while( firstNode != lastNode)
    {
        if ( firstNode->data != lastNode->data)
        {
            return 0;
        }
        firstNode = firstNode->next;
        lastNode  = lastNode->prev;
    }
    
    return 1;
}

- som February 28, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

result=True
for  i in range(len(a)/2):
 if a[i] != a[-i-1]:
  result = False
  print "Not Palindrome"
  break
if result:
 print "Test Pass"

- Anonymous April 06, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean isPalindrome() {
		boolean isPalindrome = true;
		while(first.Next != null && last.Prev!= null) {
			if(first.data != last.data) 
				return false;
			else if(first == last)
				break;
			else
			{
				first = first.Next;
				if(first == last)
					break;
				last = last.Prev;
			}
		}
		return isPalindrome;
		
	}

- undefined April 30, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//file: Link_node.java:

public class Link_Node {
	char letter;
	Link_Node next;
	Link_Node prev;
	
	public Link_Node (char letter) {
		this.letter =letter;
	}
	
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        Link_Node current = this;
        while (current != null) {
            sb.append(current.letter + "->");
            current = current.next;
        }
        return sb.toString();
    }
}   

//file Main.java:

public class Main {
    public static void main(String[] args) {
    	
        Link_Node a = new Link_Node('a');
        Link_Node b = new Link_Node('b');
        Link_Node c = new Link_Node('c');
        Link_Node d = new Link_Node('b');
        Link_Node e = new Link_Node('a');
        //a<->b<->c<->d<->e
        a.next = b; b.prev = a;
        b.next = c; c.prev = b;
        c.next = d; d.prev = c;
        d.next = e; e.prev = d;
        Solution s = new Solution();
        boolean result = s.isDLLPalindrome(a);
        System.out.println(result);
     
    }
}

//file Solution.java:
//Time complexity: O(n)
//Space : O(1)

public class Solution {
   public boolean isDLLPalindrome(Link_Node first) {
	   
	    if (first == null) {return true;}
	 
	    Link_Node last = first;
	    while (last.next != null)	{
	        last = last.next;
	    }
	    
	    while (first != last)	 {
	        if (first.letter != last.letter) {
	        	return false;
	        }else {
		        first = first.next;
		        last = last.prev;
	        }
	    }
	    return true;
	}
}

- Simple Java time O(n), space O(1) August 23, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean check(Node node) {
    	
    	 if (node == null)
	            return false;
    	Node last = null; 
    	head = node;
    	boolean b = false;
    	int count = 0;
    	while(head!=null) {
    		last = head;
    		head = head.next;
    		count ++;
    	}
    	head = node;
    	while(count>0) {
    		if(head.data==last.data) {
    			head = head.next;
    			last = last.prev;
    			b = true;
    		}
    		else {
    			b = false;
    			break;
    		}
    		count--;
    	}
		return b;
    }

- goyalnamisha2010 March 03, 2019 | 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