From 9b8440949b10076f0c207d6ff82ddb274c7b39c0 Mon Sep 17 00:00:00 2001 From: Nithin Mahendran Date: Tue, 5 Oct 2021 00:35:06 +0530 Subject: [PATCH] Instagram bug fix --- .../{ => Stack using Array}/README.MD | 20 +- ...enting_stack_using_array_and_linked_list.c | 436 +++++++++--------- .../Two Sum Problem/two_sum.py | 13 + program-72/program.c | 52 --- program-72/readme.md | 3 - 5 files changed, 241 insertions(+), 283 deletions(-) rename Interview Questions/{ => Stack using Array}/README.MD (97%) rename Interview Questions/{ => Stack using Array}/implementing_stack_using_array_and_linked_list.c (95%) create mode 100644 Interview Questions/Two Sum Problem/two_sum.py delete mode 100644 program-72/program.c delete mode 100644 program-72/readme.md diff --git a/Interview Questions/README.MD b/Interview Questions/Stack using Array/README.MD similarity index 97% rename from Interview Questions/README.MD rename to Interview Questions/Stack using Array/README.MD index 65b6f3b6..02569009 100644 --- a/Interview Questions/README.MD +++ b/Interview Questions/Stack using Array/README.MD @@ -1,10 +1,10 @@ -# Interview Questions and Answers - -| Sl no. | Question | Answer link | Language | Contributor | -|---|---|---|---|---| -| 1 | Implementing **Stack** using **Array** and **Linked List** | [code here](https://github.com/nitinkumar30/basicprograms/blob/master/Interview%20Questions/implementing_stack_using_array_and_linked_list.c) | C | [Nitin Kumar](https://github.com/nitinkumar30/) - - -# Repo contributed by - -[Nitin Kumar](https://github.com/nitinkumar30/) +# Interview Questions and Answers + +| Sl no. | Question | Answer link | Language | Contributor | +|---|---|---|---|---| +| 1 | Implementing **Stack** using **Array** and **Linked List** | [code here](https://github.com/nitinkumar30/basicprograms/blob/master/Interview%20Questions/implementing_stack_using_array_and_linked_list.c) | C | [Nitin Kumar](https://github.com/nitinkumar30/) + + +# Repo contributed by + +[Nitin Kumar](https://github.com/nitinkumar30/) diff --git a/Interview Questions/implementing_stack_using_array_and_linked_list.c b/Interview Questions/Stack using Array/implementing_stack_using_array_and_linked_list.c similarity index 95% rename from Interview Questions/implementing_stack_using_array_and_linked_list.c rename to Interview Questions/Stack using Array/implementing_stack_using_array_and_linked_list.c index e103e454..772ec406 100644 --- a/Interview Questions/implementing_stack_using_array_and_linked_list.c +++ b/Interview Questions/Stack using Array/implementing_stack_using_array_and_linked_list.c @@ -1,218 +1,218 @@ -// Implementing stack using Array in C language - -// ------------------------------------------------------- - -#include -#include - -struct Stack{ - int size; - int top; - int *arr; -}; - -// ---- operations to perform : -// isEmpty- -// isFull- -// push- -// pop- -// peek- -// stackTop- -// stackBottom- - -// ------------------------------------- - -int isEmpty(struct Stack * ptr){ - if(ptr->top == -1) - return 1; - else - return 0; -} -// ------------------------------------- - -int isFull(struct Stack * ptr){ - if(ptr->top == ptr->size-1) - return 1; - else - return 0; -} -// ------------------------------------- - -void push(struct Stack *ptr, int data){ - if(isFull(ptr)){ - printf("Stack Overflow for data %d \n", data); - } - else{ - ptr->top+=1; - ptr->arr[ptr->top] = data; - } -} -// ------------------------------------- - -int pop(struct Stack *ptr){ - if(isEmpty(ptr)) - printf("Stack Underflow"); - else{ - int p = ptr->arr[ptr->top]; - ptr->top-=1; - return p; - } -} -// ------------------------------------- - -int peek(struct Stack *ptr, int i){ - int arrInd = ptr->top-i+1; - if(arrInd <= -1) - printf("Not valid position."); - else - return ptr->arr[arrInd]; - return -1; -} -// ------------------------------------- - -int stackTop(struct Stack *ptr){ - return ptr->arr[ptr->top]; -} -// ------------------------------------- - -int stackBottom(struct Stack *ptr){ - return ptr->arr[0]; -} -// ------------------------------------- - -int main(){ - struct Stack *s = (struct Stack *)malloc(sizeof(struct Stack)); - s->size=10; - s->top=-1; - s->arr=(int *)malloc(s->size*sizeof(int)); - - printf("Before pushing: Full %d\n",isFull(s)); - printf("Before pushing: Empty %d\n",isEmpty(s)); - - push(s, 1); - push(s, 2); - push(s, 3); - push(s, 4); - push(s, 5); - push(s, 6); - push(s, 7); - push(s, 8); - push(s, 9); - push(s, 10); - push(s, 11); - push(s, 12); - push(s, 13); - - printf("After pushing: Full %d\n",isFull(s)); - printf("After pushing: Empty %d\n",isEmpty(s)); - - printf("\n----------------------\n"); - printf("Peeking through stack\n"); - for(int j=1;jtop+2;j++){ - printf("The value at position %d is %d\n", j, peek(s, j)); - } - - printf("\nPopped %d from stack", pop(s)); - printf("\nPopped %d from stack", pop(s)); - printf("\nPopped %d from stack", pop(s)); - printf("\nPopped %d from stack\n\n", pop(s)); - - printf("After popping: Full %d\n",isFull(s)); - printf("After popping: Empty %d\n",isEmpty(s)); - - printf("\n----------------------\n"); - printf("Peeking through stack\n"); - for(int j=1;jtop+2;j++){ - printf("The value at position %d is %d\n", j, peek(s, j)); - } - - printf("\nStack at top now is %d", stackTop(s)); - printf("\nStack at bottom now is %d", stackBottom(s)); - - return 0; -} - -// ------------------------------------------------------------------------------------------------------ - -// Implementing Stack using Linked List in C language - -#include -#include - -struct Node{ - int data; - struct Node *next; -}; - -// isEmpty -// isFull -// push -// pop - -struct Node * top = NULL; - -void traverseLinkedList(struct Node *ptr){ - while(ptr!=NULL){ - printf("Element is %d\n", ptr->data); - ptr=ptr->next; - } -} - -int isEmpty(struct Node *top){ - if(top == NULL) - return 1; - else - return 0; -} - -int isFull(struct Node *top){ - struct Node *n = (struct Node*)malloc(sizeof(struct Node)); - if (n==NULL) - return 1; - else - return 0; -} - -int pop(struct Node *tp){ - if(isEmpty(top)) - printf("Stack Underflow"); - else{ - struct Node *p = tp; - top = (tp) -> next; - int m=p->data; - free(p); - return m; - } -} - -struct Node *push(struct Node *top, int x){ - if (isFull(top)) - return 1; - else{ - struct Node *pr = (struct Node*)malloc(sizeof(struct Node)); - pr->data = x; - pr->next = top; - top = pr; - return top; - } -} - -int main(){ - top = push(top, 3); - top = push(top, 6); - top = push(top, 11); - top = push(top, 23); - top = push(top, 47); - - traverseLinkedList(top); - - int element=pop(top); - printf("Popped element is %d \n", element); - - traverseLinkedList(top); - - return 0; - -} - - +// Implementing stack using Array in C language + +// ------------------------------------------------------- + +#include +#include + +struct Stack{ + int size; + int top; + int *arr; +}; + +// ---- operations to perform : +// isEmpty- +// isFull- +// push- +// pop- +// peek- +// stackTop- +// stackBottom- + +// ------------------------------------- + +int isEmpty(struct Stack * ptr){ + if(ptr->top == -1) + return 1; + else + return 0; +} +// ------------------------------------- + +int isFull(struct Stack * ptr){ + if(ptr->top == ptr->size-1) + return 1; + else + return 0; +} +// ------------------------------------- + +void push(struct Stack *ptr, int data){ + if(isFull(ptr)){ + printf("Stack Overflow for data %d \n", data); + } + else{ + ptr->top+=1; + ptr->arr[ptr->top] = data; + } +} +// ------------------------------------- + +int pop(struct Stack *ptr){ + if(isEmpty(ptr)) + printf("Stack Underflow"); + else{ + int p = ptr->arr[ptr->top]; + ptr->top-=1; + return p; + } +} +// ------------------------------------- + +int peek(struct Stack *ptr, int i){ + int arrInd = ptr->top-i+1; + if(arrInd <= -1) + printf("Not valid position."); + else + return ptr->arr[arrInd]; + return -1; +} +// ------------------------------------- + +int stackTop(struct Stack *ptr){ + return ptr->arr[ptr->top]; +} +// ------------------------------------- + +int stackBottom(struct Stack *ptr){ + return ptr->arr[0]; +} +// ------------------------------------- + +int main(){ + struct Stack *s = (struct Stack *)malloc(sizeof(struct Stack)); + s->size=10; + s->top=-1; + s->arr=(int *)malloc(s->size*sizeof(int)); + + printf("Before pushing: Full %d\n",isFull(s)); + printf("Before pushing: Empty %d\n",isEmpty(s)); + + push(s, 1); + push(s, 2); + push(s, 3); + push(s, 4); + push(s, 5); + push(s, 6); + push(s, 7); + push(s, 8); + push(s, 9); + push(s, 10); + push(s, 11); + push(s, 12); + push(s, 13); + + printf("After pushing: Full %d\n",isFull(s)); + printf("After pushing: Empty %d\n",isEmpty(s)); + + printf("\n----------------------\n"); + printf("Peeking through stack\n"); + for(int j=1;jtop+2;j++){ + printf("The value at position %d is %d\n", j, peek(s, j)); + } + + printf("\nPopped %d from stack", pop(s)); + printf("\nPopped %d from stack", pop(s)); + printf("\nPopped %d from stack", pop(s)); + printf("\nPopped %d from stack\n\n", pop(s)); + + printf("After popping: Full %d\n",isFull(s)); + printf("After popping: Empty %d\n",isEmpty(s)); + + printf("\n----------------------\n"); + printf("Peeking through stack\n"); + for(int j=1;jtop+2;j++){ + printf("The value at position %d is %d\n", j, peek(s, j)); + } + + printf("\nStack at top now is %d", stackTop(s)); + printf("\nStack at bottom now is %d", stackBottom(s)); + + return 0; +} + +// ------------------------------------------------------------------------------------------------------ + +// Implementing Stack using Linked List in C language + +#include +#include + +struct Node{ + int data; + struct Node *next; +}; + +// isEmpty +// isFull +// push +// pop + +struct Node * top = NULL; + +void traverseLinkedList(struct Node *ptr){ + while(ptr!=NULL){ + printf("Element is %d\n", ptr->data); + ptr=ptr->next; + } +} + +int isEmpty(struct Node *top){ + if(top == NULL) + return 1; + else + return 0; +} + +int isFull(struct Node *top){ + struct Node *n = (struct Node*)malloc(sizeof(struct Node)); + if (n==NULL) + return 1; + else + return 0; +} + +int pop(struct Node *tp){ + if(isEmpty(top)) + printf("Stack Underflow"); + else{ + struct Node *p = tp; + top = (tp) -> next; + int m=p->data; + free(p); + return m; + } +} + +struct Node *push(struct Node *top, int x){ + if (isFull(top)) + return 1; + else{ + struct Node *pr = (struct Node*)malloc(sizeof(struct Node)); + pr->data = x; + pr->next = top; + top = pr; + return top; + } +} + +int main(){ + top = push(top, 3); + top = push(top, 6); + top = push(top, 11); + top = push(top, 23); + top = push(top, 47); + + traverseLinkedList(top); + + int element=pop(top); + printf("Popped element is %d \n", element); + + traverseLinkedList(top); + + return 0; + +} + + diff --git a/Interview Questions/Two Sum Problem/two_sum.py b/Interview Questions/Two Sum Problem/two_sum.py new file mode 100644 index 00000000..7802dc8c --- /dev/null +++ b/Interview Questions/Two Sum Problem/two_sum.py @@ -0,0 +1,13 @@ +def twoSum(self, nums: List[int], target: int) -> List[int]: + + m = {} # store compliment and index of other value + + for i in range(len(nums)): + n = nums[i] + c = target - n + + if n in m: + i1 = m[n] + return [i1, i] + else: + m[c] = i \ No newline at end of file diff --git a/program-72/program.c b/program-72/program.c deleted file mode 100644 index e5d23b81..00000000 --- a/program-72/program.c +++ /dev/null @@ -1,52 +0,0 @@ -#include -#include -struct Node -{ - int data; - struct Node *next; -}*first=NULL; -void create(int A[],int n) -{ - int i; - struct Node *t,*last; - first=(struct Node *)malloc(sizeof(struct Node)); - first->data=A[0]; - first->next=NULL; - last=first; - - for(i=1;idata=A[i]; - t->next=NULL; - last->next=t; - last=t; - } -} -void Display(struct Node *p) -{ - while(p!=NULL) - { - printf("%d ",p->data); - p=p->next; - } -} -void RDisplay(struct Node *p) -{ - if(p!=NULL) - { - RDisplay(p->next); - printf("%d ",p->data); - - } -} -int main() -{ - struct Node *temp; - int A[]={3,5,7,10,25,8,32,2}; - create(A,8); - - Display(first); - - return 0; -} \ No newline at end of file diff --git a/program-72/readme.md b/program-72/readme.md deleted file mode 100644 index 3f17c521..00000000 --- a/program-72/readme.md +++ /dev/null @@ -1,3 +0,0 @@ -program 72 - -C program to create and display a linked-lists \ No newline at end of file