Alcatel Lucent Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

You can always count the number of nodes first then have another pass for removing. A more efficient way is to start with two pointers n nodes apart. when the second pointer reaches the end, return the node pointed to by the first pointer

- Anonymous May 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Test
public void deleteNthNodeFromEnd() {
LinkList lis = populateLinkList();
int n = 3;
Node pointer1 = lis.getHead();
Node pointer2 = lis.getHead();
Node pointer3 = lis.getHead();
int i = 1;
while(pointer1.getNext()!=null) {
if(i<n){
pointer1 = pointer1.getNext();
++i;
} else {
pointer1 = pointer1.getNext();
pointer3 = pointer2;
pointer2 = pointer2.getNext();
}
}
pointer3.setNext(pointer2.getNext());
lis.printList();
}





public class Node {

Node next;
Object data;

public Node(Object dataValue) {
next = null;
data = dataValue;
}

public Node(Object dataValue, Node nextValue) {
next = nextValue;
data = dataValue;
}

public Object getData() {
return data;
}

public void setData(Object dataValue) {
data = dataValue;
}

public Node getNext() {
return next;
}

public void setNext(Node nextValue) {
next = nextValue;
}
}


public class LinkList {
private Node head;

public LinkList() {
head = null;
}

public Node getHead() {
return head;
}

public void setHead(Node head) {
this.head = head;
}

public void insert(Object dataValue) {
if(head==null) {
head = new Node(dataValue);
} else {
Node newNode = new Node(dataValue,head);
head = newNode;
}
}

public boolean isEmpty(){
return head == null;
}

public void printList() {
Node currentLink = head;
System.out.print("List: ");
while(currentLink != null) {
System.out.println(currentLink.data.toString());
currentLink = currentLink.getNext();
}
System.out.println("");
}

public void delete(){
head = head.getNext();
}
}

- Anonymous May 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void deleteNthNode(Node* head, int nthNodeFromEnd)
{
Node* first = head;
Node* second = head;

i = 1;

while(i <= n)
{

first = first->next;
i++;

}

while(first->next != null)
{
first = first->next;
second = second->next;
}
second->next = second->next->next;

}

- Anonymous September 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

here is an optimum way to delete the Kth node from last..

public void deleteK(int k){
		Link<Integer> current = head;
		Link<Integer> previous = head;
		
		for(int i=0;i<k;i++){
			current = current.next;
			if(current == null)
				return;
		}
		
		while(current.next != null){
			previous = previous.next;
			current = current.next;
		}
		
		previous.next = previous.next.next;
	}

Please do let me know in case of any improvement..

- vrajendra.singh.mandloi October 06, 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