Showing posts with label interview-question. Show all posts
Showing posts with label interview-question. Show all posts

Wednesday, August 15, 2012

Reverse a linked list in group of k

This is one of a candidate interview question. I don't see any practical application of this-
Given a linked list, reverse the list in groups of size k. 
I am sure it is not clear to you, so here is a example-
Input -    1=>2=>3=>4=>5=>6=>7=>8
Output -  3=>2=>1=>6=>5=>4=>8=>7

Try solving it yourself before going through the code.
#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
int v;
struct node *next;
} node;

node * revk(node *head, int k)
{
int i = 1;
node *p, *b, *s1, *s2, *t;
p = head;
b = NULL;
s1 = NULL;
s2 = head;

while (p)
{
t = p;
p = p->next;
if (t)
t->next = b;
b = t;

if (i == k)
{
if (s1)
s1->next = b;
else
head = b;
s1 = s2;
s2 = p;
i = 0;
}
i++;
}
s1->next = b;
s2->next = NULL;
return head;
}

void print(node *head)
{
node *p = head;
while (p)
{
printf("%d -> ", p->v);
p = p->next;
}
printf("$\n");
}

void insert(node **t, int v)
{
node *p = malloc(sizeof(node));
p->v = v;
p->next = NULL;
(*t)->next = p;
*t = p;
}

int main(int argc, char *argv[])
{
node *head, *tail = NULL;
head = malloc(sizeof(node));
head->next = NULL;
head->v = 1;
tail = head;
insert(&tail, 2);
insert(&tail, 3);
insert(&tail, 4);
insert(&tail, 5);
insert(&tail, 6);
insert(&tail, 7);
insert(&tail, 8);

print(head);

head = revk(head, 3);

print(head);

tail = head;

while (tail)
{
head = tail;
tail = tail->next;
free(head);
}

printf("\n");
return 0;
}

Wednesday, July 18, 2012

The stack that could give minimum in O(1)

This is one of my favorite interview question-
You have to create a stack which gives the smallest element in constant (or O(1)) time. 
You can be asked for the largest element instead of smallest also.

Take some time to think. Or read the grayed hint below, followed by the solution and explanation-
You can use linear amount of (extra) memory.
    Ok now the solution. You take a separate stack, call it min-stack. The other stack is the main-stack. 

    When you push a element in main-stack, compare the new element with top (using peek, not pop) of min-stack. Following cases are possible-
    1. The min-stack is empty => simply push the new element in min-stack as well.
    2. The min-stack top is smaller => do nothing.
    3. The min-stack top is larger => push the new element in min-stack as well.
    4. The min-stack top is equal => push the new element in min-stack as well. 
    When you pop the element from the main-stack, again compare the popped element with the min-stack top. There are only 2 cases now-

    1. The min-stack top is equal => pop from min-stack as well.
    2. The min-stack top is not equal => do nothing
    When the time comes for get minimum, just return the top of min-stack (taking care of underflow).

    For completeness, here is the C++ code

    void stack::push(int a)
    {
    main_stack.push(a);

    if (min_stack.is_empty())
    min_stack.push(a);
    else if (a <= min_stack.top())
    min_stack.push(a);
    }

    int stack::pop()
    {
    //take care of underflow here

    int e = main_stack.top();
    if (min_stack.top() == e)
    min_stack.pop();

    return e;
    }

    int stack::minimum()
    {
    //take care of underflow here

    return min_stack.top();
    }