Interview Question
Country: United States
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;
}
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[0] == 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);
}
+---+ +---+ +---+ +---+ +---+
| |<-->| |<-->| |<-->| |<-->| |
+---+ +---+ +---+ +---+ +---+
^ ^ ^ ^
| | | |
+ + + +
[-----------------------] [------]
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;
}
Can you please elaborate.
- noname December 03, 2012