Skip to content

Instantly share code, notes, and snippets.

View anushshukla's full-sized avatar
💭
Silently and secretly building awesome applications

Anush Shukla anushshukla

💭
Silently and secretly building awesome applications
View GitHub Profile
@anushshukla
anushshukla / big-arr-find-index.ts
Created August 1, 2022 12:54
Logistics: finding a package of given weight in a shipment
/*
At logistics we deal with package weights before shipment.
Lets say there are infinite number of packages arranged in increasing order of weights.
You have to find if a package of given weight (W) exist .
If exist return position else return -1;
Infinite : You will never be able to know size of array (Arr.length/size is not possible)
eg. [2,3,6,10, 46 , 49 , 100 , 110 , . , . , . , . , …..]
*/
@anushshukla
anushshukla / arr-str-to-uniform-arr-lines.ts
Created July 30, 2022 11:28
You are given a list of words. You have a notebook which allows each line to have only ‘W’ numbers of characters.
/*
You are required :
1. pack as many words as you can in each line
2. Extra spaces between words should be distributed as evenly as possible.
3. For the last line of text, it should be left-justified
*/
const getLineOutput = (strArr: string[], maxCharsInLine: number): string[] => {
@anushshukla
anushshukla / mongo-replica-set-guidelines.md
Created July 13, 2022 02:33
Mongo Replica Set Guidelines

Why do we need it?

Adding replicas in Mongo helps us achieve high scalability and availability.

How do we setup it?

We can add secondary nodes and arbiters (for primary election purpose only) if required.

What is the possible best setup?

Best recommendation is to start with https://docs.mongodb.com/manual/core/replica-set-architecture-three-members.

At the application, connection string needs to updated with other nodes host and port while setting the readPreference=secondayPreferred. (citing Read Preference) while having readConcern=majority (citing Read Concern).

@anushshukla
anushshukla / planet-mass-stable.ts
Created July 5, 2022 21:15
Get planet to destroy to make even and odd sums of planet mass same
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
@anushshukla
anushshukla / strings-aio-prototype.ts
Created April 19, 2022 21:19
Index of anagram sub string
interface String {
aio(substr: string): number;
}
// S -> Space complexity
// T -> Time complexity
// n -> length of the string
// m -> length of the substr
// Overall complexity -> T(n+m) & S(n)
String.prototype.aio = function(substr: string): number {
@anushshukla
anushshukla / Guidelines.md
Last active December 26, 2022 06:32
Caching solutions

Problem statement

When the system performs not upto expectation in terms of UX or/and system resources utilisation.

Solutions

When to apply cache?

We need to cache data when its fetching consumes a lot of machine resources and response time doesn't meet expectation while its frequently not updated & frequently requested

@anushshukla
anushshukla / binary-tree-level-wise-node-val-avg.ts
Created April 18, 2022 03:36
[Draft] Binary tree level wise average of node values
type TypeNode = BinaryNode | null;
class BinaryNode {
public value: number;
private _leftChild: TypeNode;
private _rightChild: TypeNode;
constructor(node: number, leftChild: TypeNode = null, rightChild: TypeNode = null) {
this.value = node;
if (leftChild) {
this._leftChild = leftChild;
@anushshukla
anushshukla / is-palindrom-num.ts
Last active April 17, 2022 20:04
Solutions to Leet Code Problems
function isPalindrome(num: number): boolean {
let reversedNum = 0;
let x = num;
while (x > 0) {
reversedNum = reversedNum * 10 + x % 10;
x = x / 10 >> 0; // >> removes decimal
}
return reversedNum === num;
};
@anushshukla
anushshukla / find-max-prods-pick-from-pile-of-prods.js
Created April 16, 2022 13:40
[Draft] Find maximum products which can picked up from pile of products with increasing count of products pick up
const removeProductsFromPile = (pileOfProducts, indexStart = 0, endIndex = pileOfProducts.length, maxProducts, reduceBy = 'max') => {
pileOfProducts.map((product, currentProductIndex) => {
const productsLeftInPile = product - 1;
let lastProductsPickCount;
for (let nextProductIndex = currentProductIndex + 1; nextProductIndex < pileOfProducts.length; nextProductIndex++) {
const nextPileOfProduct = product[nextProductIndex];
if (nextPileOfProduct > product) {
maxProducts = product + nextPileOfProduct;
}
@anushshukla
anushshukla / reverse-line-as-per-word-length.js
Created April 16, 2022 13:17
Reverse the line as per word length in ascending
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;