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 (head == null)
        return head;
    if (m == 0 && n == 0)
        return head;
    // 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)
        return head;

    var fakeHead = new ListNode();
    fakeHead.Next = head;

    var current = fakeHead;
    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;
    }

    return fakeHead.Next;
}

- hnatsu March 04, 2016 | Flag Reply
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){
	if (head == NULL) return;
	list *cur = head;	
	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;
	}
}

- praveen pandit March 05, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- SHR March 10, 2016 | Flag Reply
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.

- Manku September 07, 2016 | 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