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

public ListNode plusOne(ListNode head) {
        if(head == null) return head;
        
        ListNode newHead = reverse(head);
        head = newHead;
        int sum = 1;
        while(head != null)
        {
            sum += head.val;
            head.val = sum%10;
            sum/=10;
            if(sum==0 || head.next == null)
                break;
            head = head.next;
        }
        if(sum > 0)
        {
            head.next = new ListNode(sum);
        }
        return reverse(newHead);
    }
    
    private ListNode reverse(ListNode head)
    {
        ListNode newHead = null, t;
        while(head != null)
        {
            t = head;
            head = head.next;
            t.next = newHead;
            newHead = t;
        }
        return newHead;
    }

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

I assume it's a singly linked list, and the head place has greater value than the tail place.
Looks like the requirement of O(1) space doesn't leave a lot of choice.

#include <iostream>

using namespace std;

class Node {
	public:
		Node(int val)
		{
			val_ = val;
			next_ = NULL;
		}
		int val_;
		Node *next_;
};

Node *AddDigit(Node *head, int digit)
{
	int size = 0;
	for (Node *n = head; n != NULL; n = n->next_) {
		++size;
	}
	int carry = digit;
	for (int i = size; i > 0 && carry != 0; --i) {
		Node *n = head;
		for (int j = 0; j < i - 1; ++j) {
			n = n->next_;
		}
		int sum = n->val_ + carry;
		n->val_ = sum % 10;
		carry = sum >= 10 ? 1 : 0;
	}
	if (carry != 0) {
		Node *new_head = new Node(carry);
		new_head->next_ = head;
		head = new_head;
	}
	return head;
}

void Print(Node *head)
{
	for (Node *n = head; n != NULL; n = n->next_) {
		cout << n->val_ << ", ";
	}
	cout << "\n";
}

int main()
{
	Node *head = NULL;
	for (int i = 0; i < 100; ++i) {
		head = AddDigit(head, 1);
		Print(head);
	}
    return 0;
}

- Alex November 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Node *temp = root;
while(temp->next){
temp= temp -> next;
}
if(!((temp->next->val+k)/10)){
temp->next->val = temp->next->val+k;
}
else{
temp->val = temp->val + (temp->next->val+k)/10;
temp->next->val = (temp->next->val+k)%10;
}

- Shobhit Gupta September 04, 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