## Interview Question

Country: United States

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

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

Question is not clear....are you given with head of the linked list and asked which nodes from array are pointing to this list?

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

1. Get ith pointer in the array.
2. Starting from (i+1)th pointer
2.1 check auxillary counter in order to decide continuation
2.2 if it's ith pointer's prev or next element.
2.2.1 If so increment counter.
2.2.2. Increment auxillary counter

``````struct DLL
{
int data;
struct DLL *prev;
struct DLL *next;
};

int findNumOfConNodes(struct DLL *a[], int size)
{
int connected = 0;
short fullConnected = 0; // both prev and next node is in the array

for (int i = 0; i < size; i++)
{
fullConnected = 0;

for (int j = i+1; j < size; j++)
{
if (fullConnected == 2) break; // no need to continue

if (a[i]->prev == a[j] || a[i]->next == a[j])
{
connected++;
fullConnected++;
}

}
}

return connected;
}``````

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

O(n^2). hashing the pointers can reduce it to O(n), but I was wondering if anyone had anything better.

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

heuristician - I understood the question asking for number of contiguous blocks, as opposed to whether array pointers are one contiguous block.

I can't think of anything better than O(n^2).

My proposed solution below is O(n^2) time, O(n) space complexity.

It uses the same basic approach as heuristician. It marks blocks as it scans the array of points. If it encounters an existing block, then it merges blocks and adjusts block count.

``````#define EMPTY_CELL -1

struct Node
{
Node* next;
Node* prev;
};

int ConnectedNodeBlocks(Node** nodes, int count)
{
bool increment = false;
int i, j, k;
int matchCount = 0;
int blockCount = 0;
int currentBlockNumber = 0;
int* blockNumbers = NULL;

if (nodes == NULL || count == 0)
return (blockCount);

if (count == 1)
{
if (nodes == NULL)
{
printf("error : null entry");
return (-1);
}

return (1);
}

blockNumbers = new int[count];
if (blockNumbers == NULL)
{
printf("error : out of memory");
return (-1);
}
memset(blockNumbers, EMPTY_CELL, sizeof(int)*count);

for (i = 0, blockCount = 1, currentBlockNumber = 0, increment = false ; i < count ; i++)
{
if (nodes[i] == NULL)
{
printf("error : null entry");
return (-1);
}

if (blockNumbers[i] == EMPTY_CELL)
{
blockNumbers[i] = currentBlockNumber;
increment = true;
}

for (j = i + 1, matchCount = 0 ; j < count && matchCount < 2 ; j++)
{
if (nodes[j] == NULL)
{
printf("error : null entry");
return (-1);
}

if ((nodes[i]->prev == nodes[j]) || (nodes[i]->next == nodes[j]))
{
matchCount++;

if (blockNumbers[j] == EMPTY_CELL)
{
blockNumbers[j] = blockNumbers[i];
}
else
{
int tmp;

tmp = blockNumbers[i];
blockNumbers[i] = blockNumbers[j];
blockCount--;

if (matchCount == 1)
continue;

for (k = 0 ; k < count ; k++)
{
if (blockNumbers[k] == tmp)
{
blockNumbers[k] = blockNumbers[j];
}
}
}
}
}

if (increment == true)
{
blockCount++;
currentBlockNumber++;
increment = false;
}
}

return (blockCount);
}``````

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

+---+    +---+    +---+    +---+    +---+
|   |<-->|   |<-->|   |<-->|   |<-->|   |
+---+    +---+    +---+    +---+    +---+
^        ^        ^                 ^
|        |        |                 |
+        +        +                 +
[-----------------------]           [------]
1st block                  2nd block

....,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,.... ??

Pointer list : 10,8,15,9,5,6,10  - O(N ) + Order of Serach ()
1,1,1,1,1,1,1
Best case - 4,5,6
1, 1,1
1 bucket ,

3 10  2
4 9
Number of Bucket

Bucket 10,9,8 - 1 bucket
bucket 15, -- 2 bu
bucket 5,6
3 buckets ...

ptr -> 10 value (10)
class Node {
Node left;
Node right;
int data;
}

int findNumberOfbuckets( ArrayList<Node> inputLocations)
{
int numberOfbuckets =0;
ArrayList <int> bucket = Arraylist<int>();

HashMap <int,boolen> maplist = new HashMap<int,boolean>();
for( int i =0,i<=inputLocations.length();i++) {
maplist.put(inputLocations[i],0);
}

for( int i =0,i<=inputLocations.length();i++) {
//  bucket.put(inputLocations[i]);
//maplist.put(inputLocations[i],1);
if( maplist.get((inputLocations[i].left) == 0) {
bucket.put(inputLocations[i].left)
maplist.put(inputLocations[i].left,1);
}
else {
numberOfbuckets++;
}
if( maplist.get((inputLocations[i].right) == 0) {
bucket.put(inputLocations[i].right)
maplist.put(inputLocations[i].right,1);
}
else {
numberOfbuckets++;
}
}

return numberOfbuckets;

}

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.

### 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.