Skip to content

Commit

Permalink
new question added
Browse files Browse the repository at this point in the history
  • Loading branch information
arorarahul committed Apr 18, 2017
1 parent 7b3e0f1 commit 30b56dc
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 2 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ greater than size of elements to be stored.
- Sometimes, scanning once and searching and keeping additional variables for check can do the job.
- In hashing the structure can vary as per requirements. It can store anything from sum to frequency to pointers. So decide the structure as per always.

### For Stacks & Queues: (methods that can be applied)
- For a stack and queue for each implementation or algo its core functionalities should hold true everytime.
Eg popping and pushing on a stack takes O(1) time.
- For making a stack using two queues or vice-versa either burden can be on push operation or pop operation. The one having burden will hold the task of enqueuing and dequeuing to move it to the other stack or queue.
Refer to stacks and queues question4 for more info
- For questions in stacks and queues use additional stacks/queues as datastructures to implement some algorithms
- You can store the minimum or max in other stack/queue or data structure for every push and pop and so on.
For eg storing the minimum so far in another stack so that each time when a number if popped off, if we pop off the minimum from the other stack, we will have min from the remaining elements sitting on top of the other stack. Like this many operations can be applied.
- You can also modify the numbers being pushed onto a stack and maintain an external reference by doing some computations to make an algorithm work. (Eg question5).

# General hash functions
- take mod with number of elements present

Expand Down Expand Up @@ -160,6 +170,7 @@ TODO:
- [Implement a queue using circular array](/stacks-and-queues/question2.c)
- [Implement a queue using two stacks](/stacks-and-queues/question3.c)
- [Implement a stack using queues](/stacks-and-queues/question4.c)
- [Design a stack such that get Minimum should be O(1)](/stacks-and-queues/question5.c)


## Some important concepts to solve algos better
Expand Down
2 changes: 1 addition & 1 deletion nextquestions.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@
- BST question5 hashing to be done
- hashing question7 method3 to be done using BST
- question4 hashing to be done
- stacks and queues better implementation to be done
- implement stack using two queues try both methods
- stacks and queues INFIX to POSTFIX & evaluating POSTFIX
99 changes: 98 additions & 1 deletion stacks-and-queues/question4.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,101 @@
/*
Implement a stack using queues
*/
It can be implemented in two ways
One is while pushing, pushing in one Q and while popping, popping from this Q and pushing in other Q and then
popping that one
OR
While pushing, pushing in empty Q and dequeing the other one and enqueuing in this one for every push this
can be done.
Time complexity: (depending on the method that is applied)
For each push: O(n)
For each pop: O(1)
OR
For each push: O(1)
For each pop: O(n)
*/
#include <stdio.h>
#include <stdlib.h>
#define MAX 5

void enqueue(int arr[],int data,int *front, int *rear){
int size = MAX;
*rear = (*rear+1)%size;
if(*rear == *front){
printf("overflow\n");
if(*rear != 0){
*rear = *rear-1;
}else{
*rear = size-1;
}
return;
}
arr[*rear]=data;
}

int dequeue(int arr[], int *front, int *rear){
int data;
int size = MAX;
if(*front == *rear){
printf("already empty\n");
return -1;
}else{
*front = (*front+1)%size;
data = arr[*front];
}
return data;
}

void push(int q1[],int elm, int *f1, int *r1){
enqueue(q1,elm,f1,r1);
}

int pop(int q1[],int q2[],int *f1,int *r1, int *f2, int *r2){
int temp,data;
if((*f1 == *r1) && (*f2==*r2)){
printf("already empty\n");
return -1;
}else{
while(*f1!=*r1){
temp = dequeue(q1,f1,r1);
enqueue(q2,temp,f2,r2);
}
data = temp;
while(*f2 != *r2){
temp = dequeue(q2,f2,r2);
enqueue(q1,temp,f1,r1);
}
}
return data;
}

int main(){
int q1[MAX],q2[MAX],step,elm,result;
int f1=0,r1=0,f2=0,r2=0;

while(1){
printf("1. PUSH element\n");
printf("2. POP element\n");
printf("3.EXIT \n");
scanf("%d",&step);

switch(step){
case 1: printf("enter element to be pushed\n");
scanf("%d",&elm);
push(q1,elm, &f1,&r1);
break;
case 2: result = pop(q1,q2,&f1,&r1,&f2,&r2);
if(result < 0){
printf("already empty\n");
}else{
printf("%d was deleted\n", result);
}
break;
case 3: exit(1);
break;
}
}
return 0;
}
28 changes: 28 additions & 0 deletions stacks-and-queues/question5.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Design a stack such that get Minimum should be O(1)
NOTE: When stack is to be designed in order to do something the core functionalities of the stack should
still hold in that design. Eg: Pushing and popping can be done in O(1) time in a stack. This should be
true always
METHOD1:
Maintain and another stack which will store minimum so far values.
For each push into the main stack, the min is compared and if there is a different value it is pushed onto
this stack. For every pop from the main stack if the popped off element is equal to the min, then the min is
popped off as well. If the main stack contains duplicate values, then the min so far stack should also
contain duplicate to make sure that popping off is consistent and works correctly.
Time complexity: Both pop and push in O(1)
Space complexity: O(n) //worst case
METHOD2:
Maintain a variable that will store the min value so far for every push done. For every push done,
if there is a new minimum, instead of pushing this minimum onto the stack, subtract the min so far value from
this min and push that value on the stack. Eg if minimum so far is 9 and new min is 2 push 2-9 that is -7
on the main stack. Like this make the stack.
While popping off, each time compare the number on the stack with the min so far, if the number is bigger
pop that number else popp of the min so far value. Also update the min so far value by subtracting the smaller
value on the stack from it. Eg if the min so far was 2 (which was popped off) and value on stack was -7,
new min so far value should be 2-(-7) which is 9. This way the whole algo will work.
Time complexity: O(1)
Space complexity: O(1)
*/

0 comments on commit 30b56dc

Please sign in to comment.