Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

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

void swapfirst_and_last_kth_node(node **head,int k)
{
node *p1=*head,*p2=*head;
while(p1!=NULL && k>1)
{
(p1)=(p1)->next;
k--;
}
if(k!=0 && (p1)==NULL)
{
cout<<"\n Error in the code";
return ;
}
node *p3=p1;
while((p1->next))
{
(p2)=(p2)->next;
(p1)=(p1)->next;
}
int tm=(p3)->data;
(p3)->data=(p2)->data;
(p2)->data=tm;

}

- pintuguptajuit(PINTU GUPTA) March 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

thanks

- cenarocks241 April 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

First finding the nth node from end
then traversing upto nth node from start
then swaping data of both the nodes found above.

internal void SwapKthnodefromFirstandKthNodefromlast(int n)
{
if (head == null || n < 1)
return;
Node p1 = head;
Node p2 = head;
for (int i = 0; i < n; i++)
p1 = p1.next;
while (p1 != null)
{
p1 = p1.next;
p2 = p2.next;
}
p1 = head;
for (int i = 0; i < n-1; i++)
{
p1 = p1.next;
}
int temp = p1.data;
p1.data = p2.data;
p2.data = temp;
p1= head;
while (p1!= null)
{
Console.WriteLine(p1.data);
p1= p1.next;
}

}

- suvi March 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

1. First locate the kth node from start and end.
a. To find the kth node from last - take two pointers pointing to head. Move 1 pointer till kth position. Then move both the pointers together till the first pointer meets the last position. At this time 2nd pointer will be pointing to the kth node from last.
b. To find the kth node from first - Move a pointer to kth position by following link k-1 times.
2. Swap the nodes.

- HSS March 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Identify kth element from beginning (say kth_first), also reverse the list.
2. Identify kth element from beginning on the reversed link list (say kth_last), and reverse the reversed list (Now the list will become original list).
3. swap the values in kth_first and kth_last.

Logic is in swap_kth_node() function. O(n) time complexity.

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    struct Node *next;
    int val;
} Node;

Node *reverse_get_kth_element(Node **head, int k)
{
    Node *kth_element = NULL, *curr = *head, *prev = NULL;
    int i = 0;
    while (curr) {
        i++;
        if (i == k)
            kth_element = curr;

        Node *tmp = curr->next;
        curr->next = prev;
        prev = curr;
        curr = tmp;
    }

    *head = prev; /* prev will be tail of original list */
    return kth_element;
}

void swap_kth_node(Node *head, int k)
{
    /* 1. find kth from first, reverse on the way */
    Node *kth_first = reverse_get_kth_element(&head, k);

    /* 2. find kth from last, reverse on the way. Then we have original list */
    Node *kth_last = reverse_get_kth_element(&head, k);

    //swap kth_first and kth_last
    int tmp_val = kth_first->val;
    kth_first->val = kth_last->val;
    kth_last->val = tmp_val;
}

Node * create_node(int val)
{
    Node *p = (Node *) malloc(sizeof(Node));
    p->next = NULL;
    p->val = val;
    return p;
}

void print_list(Node *head)
{
    printf("List is: ");
    while (head) {
        printf("%d ",head->val);
        head = head->next;
    }
    printf("\n");
}

int main()
{
    Node *head = create_node(1);
    head->next = create_node(2);
    head->next->next = create_node(3);
    head->next->next->next = create_node(4);
    head->next->next->next->next = create_node(5);
    head->next->next->next->next->next = create_node(6);
    head->next->next->next->next->next->next = create_node(7);

    print_list(head); /* 1 2 3 4 5 6 7 */

    swap_kth_node(head, 3);

    print_list(head); /* 1 2 5 4 3 6 7 */
}

- jtr.hkcr March 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/**
 * @author hissar
 */
import java.security.InvalidParameterException;

public class KthNodeSwap {

	public static <T> void swapKthNodes (LinkedNode<T> head, int k) {
		if (head == null)
			throw new NullPointerException ("Empty list");
		if (k == 0)
			throw new InvalidParameterException ("Invalid value of k");
		
		LinkedNode<T> kth = null;
		LinkedNode<T> front = null;
		LinkedNode<T> track = head;
		
		while (track != null) {
			k--;
			if (k == 0) {
				kth = head;
				front = track;
				break;
			}
			track = track.next;
		}
		if (k != 0 || track == null)
			throw new NullPointerException ("In-sufficient no. of Nodes");
		
		while (track.next != null) {
			track = track.next;
			kth = kth.next;
		}
		
		// Perform the swap
		T data = front.data;
		front.data = kth.data;
		kth.data = data;
	}
	
	public static void main(String[] args) {
		LinkedNode<Integer> head = new LinkedNode<Integer>(1);
		LinkedNode<Integer> track = head;
		for (int i = 2; i < 8; i++) {
			track.next = new LinkedNode<Integer>(i);
			track = track.next;
		}
		swapKthNodes (head, 3);
		while (head != null) {
			System.out.println(head.data.intValue());
			head = head.next;
		}
	}
}

- hissar March 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

that's a crime

throw new NullPointerException

- EK MACHCHAR May 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

so is

java.security.InvalidParameterException

- EK MACHCHAR May 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void swap(LinkedList<Integer> l, int k) {
		
		int tmp = l.get(k-1);
		l.set(k-1, l.get(l.size()-k));
		l.set(l.size()-k, tmp);
	}

- lin March 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void swapKthNode(Node head, int k){
Node kthFormBeg = head, kthFromEnd = head, temp = head;
int count = 1;
while(temp!=null){
if(count<=k){
kthFormBeg = kthFormBeg.getNext();
temp = temp.getNext();
count++;
}else{
temp = temp.getNext();
kthFromEnd = kthFromEnd.getNext();
}
}
//swap data between nodes
int t = kthFormBeg.getData();
kthFormBeg.setData(kthFromEnd.getData());
kthFromEnd.setData(t);
printList();
}

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

public void swapKthNode(Node head, int k){
		Node kthFormBeg = head, kthFromEnd = head, temp = head;
		int count = 1;
		while(temp!=null){
			if(count<=k){
				kthFormBeg = kthFormBeg.getNext();
				temp = temp.getNext();
				count++;
			}else{
				temp = temp.getNext();
				kthFromEnd = kthFromEnd.getNext();
			}			
		}
		//swap data between nodes
		int t = kthFormBeg.getData();
		kthFormBeg.setData(kthFromEnd.getData());
		kthFromEnd.setData(t);
		printList();

}

- Anonymous April 01, 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