SunGard Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

-use two pointers
-move one of the pointers n positions
-now move both the pointers simultaneously till the second pointer get's null
-ur first pointer will point to nth last element

- Sourabh September 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I only know Java, so here it is.

public class LinkedLists {
    static class Node {
        String key;
        Node   next;
    }

    public static void main(String[] args) {
        System.out.println(findNthLast(createList("a"), 1).key);
        System.out.println(findNthLast(createList("a b"), 2).key);
        System.out.println(findNthLast(createList("a b c"), 2).key);
        System.out.println(findNthLast(createList("a b c d"), 2).key);
        System.out.println(findNthLast(createList("a b c d e"), 2).key);
        System.out.println(findNthLast(createList("a b c d e f"), 2).key);
    }

    public static Node createList(String keys) {
        String[] parts = keys.split("\\s+");
        Node list = new Node();
        Node node = list;
        node.key = parts[0];
        for (int i = 1; i < parts.length; i++) {
            node.next = new Node();
            node.next.key = parts[i];
            node = node.next;
        }
        return list;
    }

    public static Node findNthLast(Node list, int n) {
        int i = 1;
        Node node = list;
        Node nthLast = null;
        while (node != null) {
            if (i == n) {
                nthLast = list;
            }
            if (i > n) {
                nthLast = nthLast.next;
            }
            node = node.next;
            i++;
        }

        return nthLast;
    }
}

- Gerry September 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I tried a simple algorithm and implemented in c++

kodeyard.blogspot.com/2011/09/nth-last-element-in-linked-list-c.html

- lipun4u September 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

FindNthLast(node *l, int n, node **ans)
{
if(l -> next == NULL) return 1;
p = FindNthLast(l->next, n, ans);
if( p == n )
*ans = l;
return p + 1;
}

- Anonymous September 22, 2011 | 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