## 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

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

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

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
}

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.tail = null;
}

let n =new node(data);
this.tail = n;
return;
}
this.tail.next = n;
n.previous = this.tail;
this.tail = n;

}

let l  =  new list();

function ispalindrome(l){
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)``````

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;

}``````

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

``````public class DLLPalindrome {

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

for(int i=0;i<dll.getSize()/2;i++)
{
{
dll.tail  = dll.tail.prev;
}
else
{
System.out.println("not a palindrome");
}
}

}

private void populateDLL() {

}

}``````

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

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

}

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

temp1 = temp1.next;
middle--;

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
def __init__(self):

newnode = Node(data)
if currnode == None:
while currnode.next is not None:
currnode = currnode.next
currnode.next = newnode
newnode.prev = currnode

def IsPalindrome(self):
tempstring_frwd= ""
tempstring_rev = ""
print "LinkedList doesn't have any nodes"
return
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"
print dl.IsPalindrome()``````

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);
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]);

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;
}``````

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"``````

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;

}``````

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

``````//file: Link_node.java:

char letter;

this.letter =letter;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
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) {

//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 {

if (first == null) {return true;}

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;
}
}``````

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;
boolean b = false;
int count = 0;
count ++;
}
while(count>0) {
last = last.prev;
b = true;
}
else {
b = false;
break;
}
count--;
}
return b;
}``````

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

``````public void checkPalindrone() {
int size=0;
Node end = null;
Boolean isPalindrone = false;
while(null!=n) {
end = n;
n=n.next;
size++;
}
for(int i =1; i<=size/2 ; i++) {
if(start.data == end.data){
isPalindrone = true;
} else {
isPalindrone = false;
break;
}
start = start.next;
end = end.previous;
}
if(isPalindrone) {
System.out.println("it is palindrone");
} else {
System.out.println("not palindrone");
}
}
}``````

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

``````public void checkPalindrone() {
int size=0;
Node end = null;
Boolean isPalindrone = false;
while(null!=n) {
end = n;
n=n.next;
size++;
}
for(int i =1; i<=size/2 ; i++) {
if(start.data == end.data){
isPalindrone = true;
} else {
isPalindrone = false;
break;
}
start = start.next;
end = end.previous;
}
if(isPalindrone) {
System.out.println("it is palindrone");
} else {
System.out.println("not palindrone");
}
}``````

}

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.

### 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.