Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Week 7 : -> Week_7 updated and files completion #9

Merged
merged 7 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Week-7/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Week - 7
---

## Linked List
### [Exercise 7.1](./exercise_7.1/Readme.md)
### [Exercise 7.2](./exercise_7.2/Readme.md)
### [Exercise 7.3](./exercise_7.3/Readme.md)


---
## Stack and Queue
### [Exercise 7.4](./exercise_7.4/Readme.md)
### [Exercise 7.5](./exercise_7.5/Readme.md)
### [Exercise 7.6](./exercise_7.6/Readme.md)

## Assignment Introduction:
* In this week student will be a given a set of problem statement on linked list
* A set of problems statement on stack and queue

## Benchmarks
* All the problem should have an optimize solutions
* All the problem statement should have the time and space complexity mention in the code comment
* The code should be readable and must follow good coding practice.
* Keep the code as modular as you can.
1 change: 0 additions & 1 deletion Week-7/Week-7

This file was deleted.

18 changes: 18 additions & 0 deletions Week-7/exercise_7.1/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## Problem 7.1 : Reverse the Linked list

Given a linked list of N nodes. The task is to reverse this list.
```
Example1: Input:LinkedList: 1->2->3->4->5->6
Output: 6 5 4 3 2 1

Explanation: After reversing the list,elements are 6->5->4->3->2->1.

Example 2: Input:LinkedList: 2->7->8->9->10
Output: 10 9 8 7 2

Explanation: After reversing the list,elements are 10->9->8->7->2.

```
Expected Time Complexity: O(N).

Expected Auxiliary Space: O(1).Constraints: 1 <= N <= 104
7 changes: 7 additions & 0 deletions Week-7/exercise_7.1/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const SinglyLinkedList = require('../lib/_linkedList');

let arr = [1, 2, 3, 4, 5, 6, 7]
const newSll = new SinglyLinkedList()
newSll.create(arr)
newSll.reverse() // reverse the List
newSll.traverse() // Present the list
30 changes: 30 additions & 0 deletions Week-7/exercise_7.2/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## Problem 7.2: Rotate Linked List

Given a singly linked list of size N. The task is to left-shift the linked list by k nodes,where k is a given positive integer smaller than or equal to length of the linked list.

```
Example 1:
Input:
N = 5
value[] = {2, 4, 7, 8, 9}
k = 3

Output: 8 9 2 4 7

Explanation:
Rotate 1: 4 -> 7 -> 8 -> 9 -> 2
Rotate 2: 7 -> 8 -> 9 -> 2 -> 4
Rotate 3: 8 -> 9 -> 2 -> 4 -> 7
```
```
Example 2:
Input: N = 8
value[] = {1, 2, 3, 4, 5, 6, 7, 8}
k = 4

Output: 5 6 7 8 1 2 3 4

```
Expected Time Complexity: O(N).

Expected Auxiliary Space: O(1).Constraints: 1 <= N <= 103 1 <= k <= N
13 changes: 13 additions & 0 deletions Week-7/exercise_7.2/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const SinglyLinkedList = require('../lib/_linkedList');

const newSll = [2, 4, 7, 8, 9]
const newSingleLinked = new SinglyLinkedList()
newSingleLinked.create(newSll)
newSingleLinked.rotate(3)
newSingleLinked.traverse() // 8 9 2 4 7

const _exp2 = [1, 2, 3, 4, 5, 6, 7, 8]
const exp2SingleLl = new SinglyLinkedList()
exp2SingleLl.create(_exp2)
exp2SingleLl.rotate(4)
exp2SingleLl.traverse() // 5 6 7 8 1 2 3 4
32 changes: 32 additions & 0 deletions Week-7/exercise_7.3/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## Problem 7.3: Detect loop in a linked list

Given a linked list of N nodes. The task is to check if the linked list has a loop. Linked list can contain self loop.

```
Example 1:
Input:
N = 3
value[] = {1,3,4}
x = 2
Output: True
Explanation: In above test case N = 3.

The linked list with nodes N = 3 is given.
Then value of x=2 is given which means last node is connected with xth node of linked list.
Therefore, there exists a loop.
```

```
Example 2:
Input:
N = 4
value[] = {1,8,3,4}
x = 0
Output: False

Explanation:
For N = 4 , x = 0 means then last Node->next = NULL, then the Linked list does not contains any loop
```
Expected Time Complexity: O(N)

Expected Auxiliary Space: O(1)Constraints: 1 ≤ N ≤ 104 1 ≤ Data on Node ≤ 103
10 changes: 10 additions & 0 deletions Week-7/exercise_7.3/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const SinglyLinkedList = require('../lib/_linkedList');

const newArr = [1, 3, 4]
const newSLL = new SinglyLinkedList()
newSLL.create(newArr)
// newSLL.createLoop()
newSLL.head.next.next

console.log(newSLL.detectLoop(1))
newSLL.traverse()
36 changes: 36 additions & 0 deletions Week-7/exercise_7.4/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## Problem 7.4: Parenthesis Checker

Given an expression string x. Examine whether the pairs and the orders of“{“,”}”,”(“,”)”,”[“,”]” are correct in exp.

For example, the function should return 'true' for exp= “[()]{}{()()}” and 'false' for exp = “[(])”.

```
Example 1:

Input:{([])}

Output:true

Explanation:{ ( [ ] ) }. Same colored brackets can form balanced pairs, with 0 number of unbalanced bracket.
```
```
Example 2:

Input:()

Output:true

Explanation:(). Same bracket can form balanced pairs, and here only 1 type of bracket is present and in balanced way.
```
```
Example 3:

Input:([]

Output:false

Explanation:([]. Here square bracket is balanced but the small bracket is not balanced and Hence , the output will be unbalanced.
```
Expected Time Complexity: O(|x|)

Expected Auxillary Space: O(|x|)Constraints: 1 ≤ |x| ≤ 32000
45 changes: 45 additions & 0 deletions Week-7/exercise_7.4/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const Stack = require('../lib/_stack');

function isBracketBalanced(_expr) {
let _stack = new Stack();
for (let i = 0; i < _expr.length; i++) {
const _x = _expr[i];
if (_x == '(' || _x == '[' || _x == '{') {
_stack.insert(_x)
continue;
}
if (_stack.length == 0) {
return false
}
let _check;
switch (_x) {
case ')':
_check = _stack.remove()
if (_check == '{' || _check == '[') {
return false
}
break;
case '}':
_check = _stack.remove()
if (_check == '(' || _check == '[') {
return false
}
break;
case ']':
_check = _stack.remove()
if (_check == '{' || _check == '(') {
return false
}
break;
default:
break;
}
}
return _stack.isEmpty
}
const expr = '{([}'
if (isBracketBalanced(expr)) {
console.log(true)
} else {
console.log(false);
}
26 changes: 26 additions & 0 deletions Week-7/exercise_7.5/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## Problem 7.5: Next Greater ELement

Given an array arr{} of size N having distinct elements, the task is to find the next greater element for each element of the array in order of their appearance in the array.
Next greater element of an element in the array is the nearest element on the right which is greater than the current element. If there does not exist next greater of current element, then next greater element for current element is -1. For example, next greater of the last element is always -1.
```
Example 1:
Input:
N = 4,
arr[] = [1 3 2 4]
Output: 3 4 4 -1

Explanation:In the array, the next larger element to 1 is 3 , 3 is 4 , 2 is 4 and for 4 ? since it doesn't exist, it is -1.
```
```
Example 2:
Input:
N = 5,
arr[]= [6 8 0 1 3]
Output: 8 -1 1 3 -1

Explanation:
In the array, the next larger element to 6 is 8, for 8 there is no larger elements hence it is -1, for 0 it is 1 ,for 1 it is 3 and then for 3 there is no larger element on right and hence -1.
```
Expected Time Complexity : O(N)

Expected Auxillary Space : O(N)Constraints: 1 ≤ N ≤ 106 1 ≤ Ai ≤ 1018
30 changes: 30 additions & 0 deletions Week-7/exercise_7.5/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

function printNGE(_arr) {
const _res = new Array().fill(0);
const _stack = new Array().fill(0);
if (!_arr || _arr.length < 1) {
return _res;
};
for (let i = 0; i < _arr.length; i++) {
while (_stack.length > 0 && _arr[_stack[_stack.length - 1]] < _arr[i]) {
const small = _stack.pop();
_res[small] = _arr[i];
};
_stack.push(i);
}
for (let i = 0; i < _arr.length; i++) {
while (_stack.length > 0 && _arr[_stack[_stack.length - 1]] < _arr[i]) {
const small = _stack.pop();
_res[small] = _arr[i];
};
}
const _rem = _stack.length;
for (let i = 0; i < _rem; i++) {
_res[_stack.pop()] = -1;
}
return _res;
}

const newArray = [1, 3, 2, 4];

console.log(printNGE(newArray)) // [ 3, 4, 4, -1 ]
31 changes: 31 additions & 0 deletions Week-7/exercise_7.6/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## Problem 7.6: Implement a Queue using 2 stack

Implement a Queue using 2 stacks s1 and s2 .

A Query Q is of 2 Types
(i) 1 x (a query of this type means pushing 'x' into the queue)
(ii) 2 (a query of this type means to pop element from queue and print the poped element)
```
Example 1:

Input:
5
1 2 1 3 2 1 4 2

Output:2 3

Explanation:In the first test case 1 2 the queue will be {2}
1 3 the queue will be {2 3} 2 poped element will be 2 the queue will be {3}1 4 the queue will be {3 4} 2 poped element will be 3.
```
```
Example 2:
Input:41 2 2 2 1 4
Output:2 -1

Explanation:
In the second test case1 2 the queue will be {2}2 poped element will be 2 and then the queue will be empty 2 the queue is empty and hence -11 4 the queue will be {4}.
```

Expected Time Complexity : O(1) for push() and O(N) for pop() or O(N) for push() andO(1) for pop()

Expected Auxilliary Space : O(1).Constraints: 1 <= Q <= 100 1 <= x <= 100
17 changes: 17 additions & 0 deletions Week-7/exercise_7.6/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const Queue = require('../lib/_queue');

let twoStackQueue = new Queue();

twoStackQueue.enqueue(1);
twoStackQueue.enqueue(2);
twoStackQueue.enqueue(2);
twoStackQueue.enqueue(2);
twoStackQueue.enqueue(1);
twoStackQueue.enqueue(4);
twoStackQueue.dequeue();

twoStackQueue.dequeue();
twoStackQueue.dequeue();


console.log(twoStackQueue.peek())
Loading