Skip to content

Commit e9857a4

Browse files
committed
Added Questions from PayTM,Amazon,Samsung,Microsoft...
1 parent ed1e914 commit e9857a4

File tree

7 files changed

+376
-0
lines changed

7 files changed

+376
-0
lines changed

Diff for: javascript/DataStructures/LinkedList/DetectLoop.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Asked by - PayTM, Amazon, MakeMyTrip, Snapdeal, VMWare, Samsung, Qualcomm, Walmart.
3+
4+
Given a linked list, check if the the linked list has a loop.
5+
6+
Create a function that returns true if a linked list contains a cycle, or false if it terminates
7+
Usually we assume that a linked list will end with a null next pointer, for example:
8+
A -> B -> C -> D -> E -> null
9+
A 'cycle' in a linked list is when traversing the list would result in visiting the same nodes over and over
10+
This is caused by pointing a node in the list to another node that already appeared earlier in the list. Example:
11+
A -> B -> C
12+
^ |
13+
| v
14+
E <- D
15+
Example code:
16+
const nodeA = new Node('A');
17+
const nodeB = nodeA.next = new Node('B');
18+
const nodeC = nodeB.next = new Node('C');
19+
const nodeD = nodeC.next = new Node('D');
20+
const nodeE = nodeD.next = new Node('E');
21+
hasCycle(nodeA); // => false
22+
nodeE.next = nodeB;
23+
hasCycle(nodeA); // => true
24+
Constraint 1: Do this in linear time
25+
Constraint 2: Do this in constant space
26+
Constraint 3: Do not mutate the original nodes in any way
27+
Hint: Search for Floyd's Tortoise and Hare algorithm.
28+
*/
29+
30+
function Node(data){
31+
this.data = data
32+
this.next = null
33+
}
34+
35+
// You can read more on cycle detection at https://en.wikipedia.org/wiki/Cycle_detection
36+
// but the main takeaway is that if hare moves twice as fast as tortoise
37+
// then a loop would be identifiable as the hare will eventually catch up with the tortoise.
38+
39+
const hasCycle = (head) => {
40+
let tortoise = head
41+
let hare = head
42+
do{
43+
if(hare.next === null)
44+
return false
45+
hare = hare.next
46+
if(hare.next === null)
47+
return false
48+
hare = hare.next
49+
tortoise = tortoise.next
50+
}while(tortoise !== hare)
51+
return true
52+
}
53+
54+
const n1 = new Node('A');
55+
const n2 = n1.next = new Node('B');
56+
const n3 = n2.next = new Node('C');
57+
const n4 = n3.next = new Node('D');
58+
const n5 = n4.next = new Node('E');
59+
console.log(hasCycle(n1)); // => false
60+
n5.next = n2;
61+
console.log(hasCycle(n1)); // => true
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Asked by - Samsung, Oracle, Amazon
3+
4+
Implement a Queue using Linked List.
5+
*/
6+
7+
let Queue = {front:null, back:null} // Initialize empty queue.
8+
9+
// constructor to implement a LL.
10+
function Node(data,next){
11+
this.data = data
12+
this.next = next
13+
}
14+
15+
// Adding elements to the queue with time complexity - O(1)
16+
function Enqueue(element){
17+
let N = new Node(element,null)
18+
if(Queue.back === null){
19+
Queue.front = N
20+
Queue.back = N
21+
}else{
22+
Queue.back.next = N
23+
Queue.back = Queue.back.next
24+
}
25+
}
26+
27+
// Remove the front element from the Queue with time complexity - O(1)
28+
function Dequeue(){
29+
if(Queue.front !== null){
30+
let first = Queue.front
31+
Queue.front = Queue.front.next
32+
return first.data
33+
}else{
34+
if(Queue.back !== null)
35+
Queue.back = null
36+
return `Queue is empty`
37+
}
38+
}
39+
40+
Enqueue(1);
41+
Enqueue(2);
42+
Enqueue(3);
43+
Dequeue();
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Asked by - Samsung, Microsoft, Codenation.
3+
4+
Implement Stack using LinkedList.
5+
*/
6+
7+
class Node{
8+
constructor(data){
9+
this.data = data
10+
this.next = null
11+
}
12+
}
13+
14+
class Stack{
15+
constructor(){
16+
this.top = null
17+
}
18+
push(item){
19+
let node = new Node(item)
20+
if(this.top){
21+
node.next = this.top
22+
this.top = node
23+
}else{
24+
this.top = node
25+
}
26+
}
27+
pop(){
28+
if(this.top){
29+
let itemToPop = this.top
30+
this.top = this.top.next
31+
return itemToPop.data
32+
}else{
33+
console('Stack is empty!')
34+
return false
35+
}
36+
}
37+
peek(){
38+
if(this.top) {
39+
return this.top.data
40+
}else{
41+
return null
42+
}
43+
}
44+
}
45+
46+
let stack = new Stack()
47+
stack.push(10) // top: 10
48+
stack.push(20) // top: 20
49+
stack.push(30) // top: 30
50+
stack.push(40) // top: 40
51+
stack.pop() // top: 30
52+
stack.peek() // 30
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Asked by - Flipkart, Adobe, Microsoft, VMWare, Samsung ...
3+
4+
Given a singly linked list, find middle of the linked list.
5+
For example, if given linked list is 1->2->3->4->5 then output should be 3.
6+
7+
If there are even nodes, then there would be two middle nodes,
8+
we need to print second middle element. For example,
9+
if given linked list is 1->2->3->4->5->6 then output should be 4.
10+
*/
11+
12+
13+
//constructor - called every time a new object is created.
14+
function Node(data,next){
15+
this.data = data
16+
this.next = next
17+
}
18+
19+
// setup some nodes and connect them to each other.
20+
// the linked list looks like:
21+
// (head) n5 -> n4 -> n3 -> n2 -> n1 -> null
22+
23+
let n1 = new Node("Bapusaheb", null)
24+
let n2 = new Node("makes", n1)
25+
let n3 = new Node("lame", n2)
26+
let n4 = new Node("memes", n3)
27+
let n5 = new Node("everytime", n4)
28+
29+
// assign a node as the head of the list.
30+
let head = n5
31+
32+
// here we use two pointers, fastPointer and slowPointer where fastPointer is incremented twice
33+
// while slowPointer is incremented once. when fastPointer reaches the end of the list,slowPointer will be
34+
// pointing to the middle of the linked list. Voila!
35+
let fastPointer = head
36+
let slowPointer = head
37+
while(fastPointer.next !== null && fastPointer.next.next !== null){
38+
fastPointer = fastPointer.next.next
39+
slowPointer = slowPointer.next
40+
}
41+
42+
slowPointer.data // returns the data in the middle of the linked list. ;-)

Diff for: javascript/DataStructures/LinkedList/Palindrome.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Asked by - Microsoft, Amazon, Snapdeal.
3+
4+
Given a singly linked list of integers,
5+
Your task is to complete the function isPalindrome that returns true
6+
if the given list is palindrome, else returns false.
7+
*/
8+
9+
// TODO :
10+
// Create a new copy of the linked list.
11+
// Reverse the newly created linked list.
12+
// Compare the original linked list with the reversed linked list.
13+
14+
function Node(data){
15+
this.data = data
16+
this.next = null
17+
}
18+
19+
Node.prototype.add = function(data){
20+
const end = new Node(data)
21+
let current = this
22+
while(current.next !== null)
23+
current = current.next
24+
current.next = end
25+
return end
26+
}
27+
28+
const isPalindrome = (list) => {
29+
const reversed = reverseAndClone(list)
30+
return isEqual(list, reversed)
31+
}
32+
33+
const reverseAndClone = node => {
34+
let head = null
35+
while(node){
36+
const copy = new Node(node.data)
37+
copy.next = head
38+
head = copy
39+
node = node.next
40+
}
41+
return head
42+
}
43+
44+
const isEqual = (one, two) => {
45+
while(one && two){
46+
if(one.data !== two.data)
47+
return false
48+
one = one.next
49+
two = two.next
50+
}
51+
return one === null && two === null
52+
}
53+
54+
const list = new Node(5)
55+
list.add(4)
56+
list.add(3)
57+
list.add(4)
58+
list.add(5)
59+
isPalindrome(list) // true
60+
61+
62+
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Asked by - Cognizant, Cisco, Adobe, Microsoft, MakeMyTrip, Snapdeal, SAP labs, Qualcomm, PayTM,VMWare ...
3+
4+
Given pointer to the head node of a linked list, the task is to reverse the linked list.
5+
6+
Input: You need to complete a method reverse() that takes head as argument and returns new head.
7+
Output: Reverse the linked list and return head of the modified list.
8+
*/
9+
10+
// TESTCASE :
11+
// let input = {
12+
// value: 1,
13+
// next: {
14+
// value: 2,
15+
// next: {
16+
// value: 3,
17+
// next: {
18+
// value: 4,
19+
// next: null
20+
// }
21+
// }
22+
// }
23+
// }
24+
25+
// reverse(input) - iterative
26+
// reverse(input,1) - recursion
27+
// reverse(input,) - third approach
28+
29+
// 1. Iterative Approach.
30+
const reverse = (head) => {
31+
let result = null
32+
let stack = []
33+
let current = head
34+
while(current){
35+
stack.push(current)
36+
current = current.next
37+
}
38+
// set head to the end of the linkedlist.
39+
result = stack.pop() || []
40+
current = result
41+
while(current){ // pop the rest of the data.
42+
current.next = stack.pop()
43+
current = current.next
44+
}
45+
return result
46+
}
47+
48+
// 2. Recursive Approach.
49+
const reverse = (node,parent) => {
50+
let result = parent || {}
51+
if(node){
52+
let child = node.next
53+
node.next = parent
54+
result = reverse(child,node)
55+
}
56+
return result
57+
}
58+
59+
// 3. Time to impress the interviewer ;-)
60+
const reverse = (input,reversedList) => input === null ? reversedList
61+
: reverse(input.next, Object.assign(input,{next:reversedList || null}))
62+
63+
// Object.assign - MDN Docs (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign)
64+
65+
66+
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Given a singly linked list, rotate the linked list counter-clockwise by k nodes.
3+
Where k is a given positive integer smaller than or equal to length of the linked list.
4+
For example, if the given linked list is 10->20->30->40->50->60 and k is 4,
5+
the list should be modified to 50->60->10->20->30->40.
6+
*/
7+
8+
// Input -
9+
let input = {
10+
value: 10,
11+
next: {
12+
value: 20,
13+
next: {
14+
value: 30,
15+
next: {
16+
value: 40,
17+
next: {
18+
value: 50,
19+
next: {
20+
value: 60,
21+
next: null
22+
}
23+
}
24+
}
25+
}
26+
}
27+
}
28+
29+
const rotate = (head,k) => {
30+
let previousHead = head,
31+
previous = head,
32+
current = head,
33+
i = 1;
34+
while(current.next){
35+
if(i === k+1){
36+
head = current
37+
previous.next = null
38+
}
39+
previous = current
40+
current = current.next
41+
i++
42+
}
43+
current.next = previousHead
44+
return head
45+
}
46+
47+
48+
49+
50+
rotate(input,3)

0 commit comments

Comments
 (0)