Skip to main content
0:01est. 18 min

Ninjacart

/*
ninja cart
3 tech + hm

1hr each
problem solving =react,js
- 19th Feb, 4 to 5
- ui design react, js
- system desighn - lld, hld
- 4 hm
*/

/**
* Basic Javascript like spread op, rest param, map, foreach, reduce, currying, closure
* Why use React?
Hooks?
Redux?
Virtual DOM?
Basic react questions and hands-on

Roun - 1 - Basic Leetcode medium question
Round-2 - LLD question based on realtime use case. The interviewer discussed the actual problem which he was going to work
LLD of ticket management system within the organization and should have approval hierarchy.
and 1 Managerial round
leetcode - Like Minimum Number of Platforms Required

find a element in pivoted sorted array
given list of strings combine them as anagrams
design scalable database, sharding , how efficiently we can generate ids keeping concurrency in mind

given a matrix print path from source to destination but u can only move in L shape

Move all the zeros to right

longest palindromic subsequence based on Dp. Other questions on OS and Database design.

Heap sort
Binary searchable elements
Bst traversals

what are the use cases of stacks

*Longest positive number subsequence.
*Right view of a binary tree.
*Move all 0s in the array to right.
*Reverse a linked list.

Find the first non repeating character in a string.

Print an array in reverse order using recursion

Longest Palindrome in a string.
Sorting.
Projects.

Data structures - Tries, 2-3 trees and other types of Trees.


*/

/**ROUND 1
*
* /**
Part 1

Create a program that measures server latency periodically.
The program receives one latency (ping) value at a time and
after each value is received, calculates the average latency
for the latest K values.

Example values:

50, 60, 70, 50, 100, 10, ...more values to come later
K = 5

If we dont have enough values, return -1 in getAverage()
*/
/**
Part 2
Say we want to omit top 2 highest values in the current last K values
to calculate the average. This is to take into account latency spikes.
Adjust your code to take this into consideration.
*/

// https://leetcode.com/discuss/interview-question/537232/google-phone-average-of-k-numbers-in-a-stream
// https://leetcode.com/problems/finding-mk-average/description/
// https://leetcode.com/problems/finding-mk-average/solutions/3070115/javascript-splay-tree-780ms/
// https://www.geeksforgeeks.org/introduction-to-splay-tree-data-structure/
// https://www.javatpoint.com/splay-tree
class LatencyMonitor {
constructor(k) {
// Implement this
this.size = k;
this.values = [];
}

// O(n)
addValue(value) {
// Implement this
// LRU
// Set()
if (this.values.length === this.size) {
this.values.shift(); // O(n)
}
this.values.push(value); // O(1)
}

// O(n)
getAverage() {
// Implement this
if (this.values.length!==this.size) {
return -1;
}
const sum = this.values.reduce((acc, v) => acc+v ,0); // iteration
return sum/this.size;
}

getKValue(type) {
switch(type) {
case 'percentage':
return (this.size - this.size*.5);
default:
return this.size-2;
}

}

// n*Log(n)
getAverageWithOmit() {
// Implement this
if (this.values.length!==this.size) {
return -1;
}
const newSize = getKValue();
const copyValues = [...this.values]; // O(n)
copyValues.sort((a, b) => a-b); // n*Log(n)
const sum = copyValues.slice(0, newSize).reduce((acc, v) => acc+v ,0); // n
return sum/newSize;
}
}

let latencyMonitor = new LatencyMonitor(5)
latencyMonitor.addValue(50)
latencyMonitor.addValue(60)
latencyMonitor.addValue(70)
console.log(latencyMonitor.getAverageWithOmit()) // -1
latencyMonitor.addValue(80)
latencyMonitor.addValue(90)
console.log(latencyMonitor.getAverageWithOmit()) // 70
latencyMonitor.addValue(100)
console.log(latencyMonitor.getAverageWithOmit()) // 80



// ROUND 2

// [1,0,1,0,0]
// [1,1,0,0,0]

const arr = [1,1,0,0,0];

let zeroCount = 0;
for (let i=0; i<arr.length;i++) {
if (arr[i] == 0) {
zeroCount++;
}
}

for (let i=0; i<arr.length;i++) {

// 5-1-3 = 1 // else --> 1
// 5-1-3 = 2 // else -->
// 5-1-3 = 3 // if -->0
if (i>=arr.length-zeroCount) {
arr[i] = 0;
} else {
arr[i] = 1;
}

}

console.log(arr);


const config = {
delay: 2000,
allowedChances: 2,
}

const cache = {
// url: 3
}

function setCacheInfo(url) {
if (cache.hasOwnProperty(url)) {
cache[url] = ++cache[url];
} else {
cache[url] = 1;
}
}

// API, if fail - retry x times, delay
async function APICall (url) {
setCacheInfo(url);
try {
const response = await fetch(url);
const data = await response.json();
console.log(data);
} catch (error) {
retryAfterDelay(url);
}
}

function retryAfterDelay(url) {
if (cache[url] > config.allowedChances) {
return new Error('Exceeded retry calls');
}

setTimeout(() => {
APICall(url);
}, config.delay)
}


// Task 3

// implement deep equal `_.isEqual()`
// implement your own version of deep equal isEqual? The lodash version covers a lot of data types. In this problem, you are asked to support :

//1. primitives
//2. plain objects (object literals)
//3. array

//3. Objects are compared by their own, not inherited, enumerable properties -- not considered

const a = {a: 'bfe'}
const b = {a: 'bfe'}

console.log('isEqual(a, b)', isEqual(a, b)) // true
console.log('a === b', a === b) // false

const c = [1, a, '4']
const d = [1, b, '4']

console.log('isEqual(c, d)', isEqual(c, d)) // true
console.log('c === d', c === d) // false


// isEqual(c, 1) // Not checking this

function isEqual(source, destination) {
// assumption both source and destination will be of same type
let isSame = true;

// Array
if (Array.isArray(source) && Array.isArray(destination)) {
if (source.length === destination.length) {
for(let i=0; i<source.length;i++) {
if (typeof source[i] === 'object') { // object inside array
if (!isEqual(source[i],destination[i])) {
isSame = false;
break;
}
} else if (source[i] !== destination[i]) { // primitive inside array
isSame = false;
break;
}
}
} else {
isSame = false;
return isSame;
}

} else if (typeof source === "object" && typeof destination === "object") { // to check object
if (Object.keys(source).length !== Object.keys(destination).length) {
isSame = false;
}
for (let prop in source) {
if (!destination.hasOwnProperty(prop)) {
isSame = false;
break;
} else if (source[prop] !== destination[prop]) {
isSame = false;
break;
}
}
} else {
// both are primitive
if (source!=destination) {
isSame = false;
}
}


return isSame;
}

```js
// https://stackoverflow.com/questions/53028735/how-can-i-retry-function
function retry(fn, attempts = 3, delay = 2000) {
return async function(...args) {
for(let i = 0; i < attempts; i++) {
try {
await fn.call(this, ...args); // delegate
} catch (e) {
if(attempts > 0) await new Promise(r => setTimeout(r, delay));
else throw e;
}
}
}
}
let retried = retry(fn);
// ... then later

await retried({ ... params });
```