I would guess the question sais, you are given a set of pointers from multiple doubly linked lists and you need to determine how many doubly linked lists the pointers come from:

assuming that the first and last nodes are special in the list in a way their next and previous pointers are null, I can simply count the nodes that only have one incoming edge, which means I have the number of start and end nodes. Divide that by two should lead to the result.

Given by experienced engineers/interviewers from FB, Google and Uber,our ONE TO ONE courses cover everything in an interview includinglatest interview questions sorted by companies,SYSTEM DESIGN Courses (highly recommended for people interviewing with FLAG) ALGORITHMS (conquer DP, Graph, Greedy, String and other advanced algo problems), and mock interviews.

Our students got offers from G, U, FB, Amz, Yahoo and other top companies after weeks of training.

Welcome to email us aonecoding@gmail.com with any questions. Thanks for reading.

I couldnt follow the question eitherBased on my understanding of the question1) Look for independant blocks of the list.for a given list, the independant block is the set of nodes until it merged into an existing list. 2) assumption: circular list but without loops. 3) if list at index i, has a node (any node, may be head node) point to a list at index k where k > i, the list k is considered a subset of list i, and list k will return pair<k, 0>4) a list k without any node will return <k, 0> , thus requiring checking whether the 0 case is if the list is empty or exist entirely into an existing list

Let me start by saying thanks to ChrisK as this solution is based on his however his solution doesnt take care of circular linked list. Here is the solution that will take care of that.THis solution runs in O(N) time and O(N) memory.

public class DoublyLinkedListHelper {
public int getNumLinkedLists(List<Pointer> pointers) {
if(pointers == null || pointers.size() == 0) {
return 0;
}
// This set will contain the individual nodes belonging to one/same linkedlist
Set<Set<Node>> setOfLinkedListNodes = new HashSet<>();
for(Pointer p : pointers) {
Iterator<Set<Node>> itr = setOfLinkedListNodes.iterator();
boolean addNewSet = true;
Set<Nodes> matchedSet = null;
while(itr.hasNext) {
Set<Node> nodes = itr.next();
// Means we found an existing linked list node set where we can add the unadded nodes
// There is a reason we havent overriden equals method as we want matching based on actual object reference
if(nodes.contain(p.from) || nodes.contain(p.to)) {
// This means that we had already found a matching list thus there is no need to keep this list
// since it will end up joining the original list
if(!addNewSet) {
// Since two linked list are joining here we need to copy the elements from one to another before we remove it;
matchedSet.addAll(nodes);
itr.remove();
// We will never have a case where a pointer is part of more than 2 sets as they must have been joined otherwise.
break;
}
else {
// Add the missing node
nodes.add(nodes.contain(p.from) ? p.to : p.from);
addNewSet = false;
matchedSet = nodes;
}
}
}
if(addNewSet) {
Set<Node> nodes = new HashSet<>();
nodes.add(p.from);
nodes.add(p.to);
setOfLinkedListNodes.add(nodes);
}
}
return setOfLinkedListNodes.size();
}
public static class Pointer {
public Node from;
public Node to;
}
public static class Node {
public int value;
public Node prev;
public Node next;
}
}

I assume the input is not really nodes of the linked list, but some abstract pointers. A pointer only tells us that there is a connection (either using next or prev of a linked in node) between two nodes.
In this case terminal nodes will be mentioned exactly 2 times in the pointers. Interior nodes will be mentioned 4 times. Each independent block contains 2 terminal nodes (head and tail).

What's Going On

Books

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.

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.

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.