Dover Organization Interview Question for Developer Program Engineers


Country: India
Interview Type: In-Person




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

package com.career.ds.linkedlist;

public class NthNodeFromEnd {
    public static void main(String args[]) {
        CreateList ll = new CreateList();
        Node head = ll.createList(7, 1);
        ll.displayList(head);
        Node nthNode = ll.getNthNodeFromEnd(head, 2);
        System.out.println();
        System.out.println(2 + " node from end:" + nthNode.getData());
    }

}

class Node {
    public Node(int data) {
        this.data = data;
    }

    int data;
    Node next;

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;       
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}

class CreateList {

    public Node createList(int length, int data) {
        Node head = new Node((data++));
        Node pointer = head;
        for (int i = 1; i < length; i++) {
            Node temp = new Node((data++));
            pointer.setNext(temp);
            pointer = temp;
        }
        return head;
    }

    public void displayList(Node head) {
        while (head != null) {
            System.out.print(head.getData() + "->");
            head = head.getNext();
        }
    }

    public Node getNthNodeFromEnd(Node head, int nth) {
        Node nthNode = head;
        Node pointer = nthNode;
        int i = 0;
        while (nthNode != null && i != nth) {
            pointer = pointer.getNext();
            i++;
        }

        while (pointer != null) {
            pointer = pointer.getNext();
            nthNode = nthNode.getNext();
        }

        return nthNode;
    }
}

- Anonymous April 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use two pointers:p1 and p2
At first ,p1 point to the Nth node from the head,p2 point to the head.
Then,p1 and p2 move to their next node together,when p1 point to the end,p2 point to the Nth node from the end.

- kurskk141 April 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

two pointer: the pfirst goes n steps first then with psecond from start together, when the pfirst arrives at the end, the psecond point the nth node form the end.

- milo April 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>

using namespace std;

struct Node
{
int mData;
Node* mNext;

Node(int data , Node* next=nullptr):mData(data),mNext(next) {}
};
struct List
{
List():mHead(nullptr){}

Node* mHead;
void Add(int data)
{
if(!mHead)
mHead = new Node(data);
else
{
Node* temp = mHead;
while(temp->mNext != nullptr)
temp = temp->mNext;
temp->mNext = new Node(data);
}
}
void Display()
{
Node *temp = mHead;
cout<<"\n Content of List are : ";
while(temp != nullptr)
{
cout<<"\t"<<temp->mData;
temp = temp->mNext;
}
}
void Find_Last_nth_Element(int count)
{
if( (count>0) && mHead)
{

Node* temp = mHead;
int index = 0;
while( (index < count) && temp )
{
temp = temp->mNext;
++index;
}
if(index != count)
cout<<"\n lesser elements in List"<<endl;
else
{
Node* nthNode = mHead;
while(temp)
{
temp = temp->mNext;
nthNode = nthNode->mNext;
}
cout<<"\n the "<<count<<" node from last in list is "<<nthNode->mData;
}
}
}
};



void main()
{
cout<<"\n Program Started"<<endl;
List myList;
for(int i=0;i<20;++i)
myList.Add(10*i+i);

myList.Display();
int nthNode=0;
cout<<"\nEnter nth Element from last:";
cin>>nthNode;

myList.Find_Last_nth_Element(nthNode);
cout<<"\n Program Ended"<<endl;
}

- shailendra.rajput November 05, 2014 | 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