Student Interview Question for Interns


Team: java
Country: United States
Interview Type: In-Person




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

public void findNthNode(int n){
		TNode curr=head;
		TNode temp=head;
		for(int i =0;i<n;i++){
			temp = temp.next;
		}
		while(temp!=null){
			curr = curr.next;
			temp = temp.next;
		}
		System.out.println("..... "+curr.val);
		
	}

- Sunny February 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

The idea is to use two pointers that are of distance n.
Initially, advance the first pointer until the distance becomes n. Then advance both pointers simultaneously until the one at the end reached the end of the list. The other pointer will point to the required node.

- Anonymous February 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Base: Create two pointers p1 and p2. Move p2 N nodes forward. ( handle situation if there are less than N nodes in list)
Step: each time save p2 into pointer p3 and try to move p2 N nodes forward.
If success, set p1 = p3 and repeat step.
If not - we moved p2 k times (k<N) and reached the end, so move p1 k times forward. node(p1) will be the answer because before this step there were N nodes between p1 and p2.

- GK February 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Below is the pseudo code (language agnostic)

itr = first_node;

for (i=0; i<N;i++) {
     itr = itr->next;
}
 itr_2 = first_node;
 while (itr->next != NULL) {
     itr = itr->next;
     itr_2 = itr_2->next;
 }
 println (itr_2.val);

- Pocha February 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The above solutions are wrong, they are not solving the problem of finding the nth element from last.
1) Calculate the length of Linked List. Let the length be len.
2) Print the (len – n + 1)th node from the begining of the Linked List.

/* Function to get the nth node from the last of a linked list*/
void printNthFromLast(struct node* head, int n)
{
    int len = 0, i;
    struct node *temp = head;
 
    // 1) count the number of nodes in Linked List
    while (temp != NULL)
    {
        temp = temp->next;
        len++;
    }
 
    // check if value of n is not more than length of the linked list
    if (len < n)
      return;
 
    temp = head;
 
    // 2) get the (n-len+1)th node from the begining
    for (i = 1; i < len-n+1; i++)
       temp = temp->next;
 
    printf ("%d", temp->data);
 
    return;
}

- yoyoyo February 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi yoyoyo, In your reply traversal is done twice. Once to find the length of list and the other to find the location. This defeats the very purpose. Question is we have to traverse the list only once. Not twice.

- Sesha February 25, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi Yoyoyo,

Question is that we need to traverse list only once. Not twice. Your code is traversing the list twice. Once to find the size and next find the element.

- Sunny February 25, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Recursive solution, python. since n can be of any value, you wanna also check for non positive integer.

For each node, this function returns a 3-tuple. (outcome_value, node_count_from_the_end, whether_the_job_is_done.)

So if the call to next node returns done, then the value is done and we return the result.
Otherwise, check to see if this node is the node we want by checking the node count from the end. increment count if not done.

#nth from last node of linkedlist

class Node(object):
    def __init__(self, data, next):
        self.data = data
        self.next = next

def nth_from_last(head, n):
    (value, count, done ) = nth_from_last_aux(head, n)
    if not done:
        return None
    else:
        return value
    
def nth_from_last_aux(head, n):
    if not head:
        return None
    if not head.next: #last node
        if n == 1:
            return (head.data, None, True)
        else:
            return (None, 1, False)
        
    (value, count, done) = nth_from_last(head.next, n)
    if done: 
        return (value, count, True)
    elif count+1 == n:
        return (head.data, count, True)
    else:
        return (value, count+1, False)
        
#test
h = Node(1, Node(2, Node(3, Node(4, None))))
print nth_from_last(h, 2)

- CC February 25, 2015 | 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