Amazon Interview Question for SDE-2s


Country: United States
Interview Type: In-Person




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

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	static class LList {
		LList(int data) {
			this.data = data;
		}

		LList(int data, LList down, LList next) {
			this.data = data;
			this.down = down;
			this.next = next;
		}

		LList() {
			this.data = Integer.MAX_VALUE;
		}

		@Override
		public String toString() {
			return " " + this.data + " ";
		}

		int data;
		LList next;
		LList prev;
		LList rand;
		LList down;
	}

	public static void main(String args[]) throws Exception {
		LList head = new LList(1);
		head.next = new LList(2);
		head.next.next = new LList(3);
		//head.next.next.next = new LList(4);
		updateLinkedList(head);
	}

	public static LList updateLinkedList(LList head) {
		LList temp = head;
		LList prev = null;
		LList slow = head;
		LList fast = head;
		Stack<LList> stack = new Stack<LList>();
		int len = 0;
		while (temp != null) {
			temp = temp.next;
			len++;
		}
		while (slow != null && fast != null) {
			prev = slow;
			stack.push(slow);
			slow = slow.next;
			fast = fast.next;
			if (fast != null)
				fast = fast.next;
		}
		if (len % 2 == 0)
			prev = prev.next;
		while (!stack.isEmpty()) {
			prev.data = stack.pop().data + prev.data;
			prev = prev.next;
		}
		return head;
	}
}

- koustav.adorable August 30, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void updateList(Node node) {
	slow_ptr = head; fast_ptr = head;
	Node prev_of_slow_ptr = head;
        Node midnode = null;
        Node midnode2 = null;
        Node midnext = null;
        if(head != null && head.next != null) {
        		while(fast_ptr != null && fast_ptr.next != null) {
        			fast_ptr = fast_ptr.next.next;
        			prev_of_slow_ptr = slow_ptr;
        			slow_ptr = slow_ptr.next;
        		}
        		if(fast_ptr != null) {
        			midnode = slow_ptr;
        			slow_ptr = slow_ptr.next;
        		}
        		second_half = slow_ptr;
        		first_half = head;
        		prev_of_slow_ptr.next = null;
        		reverse();
        		
        		if(midnode != null) {
        			midnode.data += midnode.data;
	        		midnode2 = midnode;
	        		midnode2 = midnode2.next;
	        		while(midnode2 != null) {
	        			midnode2.data = midnode2.data + first_half.data;
	        			first_half = first_half.next;
	        			midnode2 = midnode2.next;
	        		}
        		} else {
        			midnext = second_half;
        			while(midnext != null) {
        				midnext.data = midnext.data + first_half.data;
        				first_half = first_half.next;
        				midnext = midnext.next;
        			}
        		}
        		
        		first_half = head;
       		reverse();
        		
        		if(midnode != null) {
        			prev_of_slow_ptr.next = midnode;
        		} else 
        			prev_of_slow_ptr.next = second_half;
        	}
}
	
void reverse() {
		Node prev = null, curr = null, next = first_half;
		if(first_half == null) return;
		while(next != null) {
			//System.out.println("Next is ::: " + next.data);
			curr = next;
			next = curr.next;
			curr.next = prev;
			prev = curr;
		}
		first_half = prev;
		head = first_half;
}

- shweta.agarwal21 August 30, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static Node updateLinkedList(Node head) {
         if (head == null) {
             return null;
         }
         Node slow = head;
         Node fast = head;
         Stack<Node> stack = new Stack<Node>();

         while (fast != null){
             stack.push(slow);
             fast = fast.next;
             if (fast != null) {
                 slow = slow.next;
                 fast = fast.next;
             }
         }

         while(!stack.isEmpty()) {
             Node temp = stack.pop();
             slow.value =  temp.value + slow.value;
             slow = slow.next;
         }

         return head;
     }

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

public static Node updateLinkedList(Node head) {
         if (head == null) {
             return null;
         }
         Node slow = head;
         Node fast = head;
         Stack<Node> stack = new Stack<Node>();

         while (fast != null){
             stack.push(slow);
             fast = fast.next;
             if (fast != null) {
                 slow = slow.next;
                 fast = fast.next;
             }
         }

         while(!stack.isEmpty()) {
             Node temp = stack.pop();
             slow.value =  temp.value + slow.value;
             slow = slow.next;
         }

         return head;
     }

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

public ListNode secondHalf(ListNode root){
		if(root == null) return null;
		
		ListNode fast = root;
		ListNode slow = root;
		Stack<Integer> stack = new Stack<Integer>();
		
		while(fast!=null && fast.next!= null){
			stack.push(slow.val);
			slow = slow.next;
			fast = fast.next.next;
		}
		
		if(fast != null)
			stack.push(slow.val);
		
		while(slow!=null && !stack.isEmpty()){
			slow.val += stack.pop();
			slow= slow.next;
		}
		
		return root;
	}

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

public ListNode secondHalf(ListNode root){
		if(root == null) return null;
		
		ListNode fast = root;
		ListNode slow = root;
		Stack<Integer> stack = new Stack<Integer>();
		
		while(fast!=null && fast.next!= null){
			stack.push(slow.val);
			slow = slow.next;
			fast = fast.next.next;
		}
		
		if(fast != null)
			stack.push(slow.val);
		
		while(slow!=null && !stack.isEmpty()){
			slow.val += stack.pop();
			slow= slow.next;
		}
		
		return root;
	}

- Coder September 19, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void Update(Node *head)
{
	int size = 0;
	for (Node *n = head; n != NULL; n = n->next_) {
		++size;
	}
	int half = size / 2;
	
	stack<int> st;
	int i = 0;
	for (Node *n = head; n != NULL; n = n->next_, ++i) {
		if (size % 2 == 1 ? i <= half : i < half) {
			st.push(n->val_);
		}
		if (i >= half) {
			n->val_ += st.top();
			st.pop();
		}
	}
}

- Alex November 19, 2017 | 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