## Amazon Interview Question for SDE-2s

Country: United States
Interview Type: In-Person

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

``````struct node* addOneTOList(struct node *head)
{

struct node *cur, *prev;
prev = nullptr;

while(cur->next != nullptr)
{
if(cur->data != 9)
prev = cur;
cur = cur->next;
}

if(cur->data != 9)
{
cur->data += 1;
} else if(prev == nullptr) //If all the node have 9
{
struct node *temp = getnode(1);
{
}
return temp;
} else {
prev->data += 1;
prev = prev->next;
while(prev)
{
prev->data = 0;
prev = prev->next;
}
}
}``````

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

I guess this question needs more clarification, some example would be good as to what is expected.

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

``````public static Node incrementList(Node head){
Node firstNine= null;
firstNine=null;
}
if(firstNine==null){
return result;
}
if(firstNine==result && firstNine.data==9){
Node temp1= new Node(1);
temp1.next= result;
result=temp1;
}
while(firstNine!=null){
if(firstNine.data==9)
firstNine.data=0;
else firstNine.data=firstNine.data+1;
firstNine=firstNine.next;
}

return result;

}``````

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

``````public Node addOneToList(Node n){
Node current=n;
Node prev=null;
int length=0;
Node last=current;
while(current!=null){
length++;
if(current.data!=9)prev=current;
last=current;
current=current.next;
}// Now last is pointing to last digit.
//  Prev is pointing to last non 9.
if(prev==null){
//case when number is all 9
while(n!=null){
n.data=0;
n=n.next;
}
}else{
prev.data=prev.data+1;
prev=prev.next;
while(prev!=null){
prev.data=0;
prev=prev.next;
}
return n;
}
}``````

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

``````public Node addOneToList(Node n){
Node current=n;
Node prev=null;
int length=0;
Node last=current;
while(current!=null){
length++;
if(current.data!=9)prev=current;
last=current;
current=current.next;
}// Now last is pointing to last digit.
//  Prev is pointing to last non 9.
if(prev==null){
//case when number is all 9
while(n!=null){
n.data=0;
n=n.next;
}
}else{
prev.data=prev.data+1;
prev=prev.next;
while(prev!=null){
prev.data=0;
prev=prev.next;
}
return n;
}
}``````

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

``````struct Node {
int data;  //digit
struct Node* next;
}

private:
public:
}

throw InvalidOperationException;

Node* prev = null;
while (cur && cur->data == 9) {
cur->data = 0;
prev = cur;
cur = cur->next;
}

if (cur)
cur->data += 1;
else
prev->next = new Node(1, null);
}``````

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

``````if (root == null)
return;

ListNode current = root, runner = root;

while (runner != null)
{
if (runner.data != 9)
{
current = runner;
}
runner = runner.next;
}

current.data = current.data + 1;
current = current.next;

while (current != null)
{
current.data = 0;
current = current.next;
}``````

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

``````class Node:
def __init__(self, value):
self.value = value
self.next = None
self.iterator = None

def iter_on(self):
yield self
if self.next:
yield from self.next.iter_on()

def __iter__(self):
self.iterator = self.iter_on()
return self

def __next__(self):
return next(self.iterator)

def __str__(self):
return "[%d]" % self.value

def __repr__(self):
return self.__str__()

def print_on(self):
print(*self.iter_on(), sep="->")

def make_list(number):
root = None
cur_node = None
for digit_str in str(number):
new_node = Node(int(digit_str))
if cur_node:
cur_node.next = new_node
cur_node = new_node

if not root:
root = cur_node

return root

left_most_changeable = -1
right_most_changeable = -1
for idx, node in enumerate(root):
if node.value < 9:
left_most_changeable = idx
right_most_changeable = idx

for idx, node in enumerate(root):
if left_most_changeable <= idx <= right_most_changeable:
node.value += 1
if node.value > 9:
node.value = 0

if left_most_changeable == -1:
new_root = Node(1)
new_root.next = root
root = new_root

return root

l = make_list(999)
l.print_on()  # [9]->[9]->[9]->[9]
l.print_on()  # [1]->[0]->[0]->[0]->[0]``````

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.