## Amazon Interview Question for SDE-2s

Team: Product Details Page
Country: India
Interview Type: In-Person

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

Only considere cases when m >= 1 and n >= 1

``````public ListNode RemoveRetain(ListNode head, int m, int n)
{
if (m == 0 && n == 0)
// If don't skip elements and delete N is an empty list
if (m == 0)
return null;
// If skyp M and don't delete elements is the list is not modified
if (n == 0)

int count;
while (current != null)
{
count = 0;
while (current != null && count < m)
{
current = current.Next;
count++;
}

if (current == null)
break;

var last = current;
count = 0;
while (current != null && count < n)
{
current = current.Next;
count++;
}

last.Next = (current != null) ? current.Next : null;
}

}``````

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

Not Much different from last

``````void smartModify(list *head, int m, int n){
while (cur != NULL){
list *mnode = NULL;
list *nnode = NULL;
int mnum = m;
int nnum = n;
while (cur != NULL && mnum > 0){
if (--mnum == 0) mnode = cur;
cur = cur->next;
}
while (cur != NULL && nnum > 0){
if (--nnum == 0) nnode = cur;
cur = cur->next;
}
if (mnode)
mnode->next = (nnode == NULL) ? NULL : nnode->next;
}
}``````

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

hantsu - Thanks for the program. Works and all edge cases covered!!

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

@praveen, It will go into infinite loop if m==0 and n==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.