Amazon Interview Question for Software Engineer / Developers






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

traverse the linked list and, when the kth element is found, fix the next-pointer of the previous and next element. of course you have to take care of the corner cases such as when there are less than k elements.

- Anonymous July 25, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No, I misunderstood the problem.

The simplest way that I can think of is,
1. count the number of elements in the list (n).
2. delete the (n - k + 1)th element of the list.

- Anonymous July 25, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Or else what you can do is find the (k+1)th node from the end using the 2 pointer technique. Then make this node point to the next to next node. Free the next node.

- Anonymous August 18, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Put two pointers p and q to head.
Traverse with q to k places.Keep p at head.
Move p and q forward,from their respective postitions till q reaches end.
link pointed by p is the required node.

- Anonymous October 17, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

keep one pointer at first element and another pointer at k th element from first and increment the pointers till second pointer reaches null ,then the first pointer is at k th element from last ,so you and find it in this way..........

- KAUSHIK January 21, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I assume it should be done in a single-pass

// assuming k is valid
ListNode* deleteKthNodeFromBack(ListNode* head, int k)
{
    if (head == NULL) return head;
    ListNode* fast (head), slow (head) slowPrev (NULL);
    while (--k) fast = fast->next;
    while (fast->next != NULL) {
          slowPrev = slow;
          slow = slow->next;
          fast = fast->next;
    }
    if (slowPrev == NULL) head = slow->next;
    else slowPrev->next = slow->next;
   
    delete slow;
    return head;
}

- fafdu July 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static Elem head; 

 public static int DeleteKth(Elem elem,Elem prev,int k) {
            int count = 0;
            if (elem != null) {
                count = DeleteKth(elem.sled,elem, k)+1;
                if (count == k) {
                    if (prev == null) head = elem.sled;
                    else prev.sled = elem.sled;
                }
            }        
            return count;
        }
// call with 
 DeleteKth(head, null, 2); where 2 is kth to be deleted

- dijana June 11, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just a few name corrections

public static int DeleteKth(Elem elem,Elem prev,int k) {
            int count = 0;
            if (elem != null) {
                count = DeleteKth(elem.next,elem,k)+1;
                if (count == k) {
                    if (prev == null) prvi = elem.next;
                    else prev.next = elem.next;
                }
            }        
            return count;
        }

- dijana June 11, 2020 | 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