Skip to content

Instantly share code, notes, and snippets.

@hassam7
hassam7 / dequeLL.js
Created October 6, 2024 22:00
Deque LL
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class DDeque {
constructor(items = []) {
this.front = null;
@hassam7
hassam7 / deque in javascript
Created October 5, 2024 17:53
deque in javascript
class Node {
constructor(value = null) {
this.value = value;
this.prev = null;
this.next = null;
}
}
class Deque {
constructor() {
@hassam7
hassam7 / Queue.js
Created October 5, 2024 17:52
Queue
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class Queue {
constructor() {
this.front = null;
@hassam7
hassam7 / mapPromises.js
Created February 24, 2024 12:04 — forked from andrew8088/mapPromises.js
Managing Promise Concurrency in JavaScript
function mapPromises(args, callback, concurrency = 3) {
const { promise, resolve } = Promise.withResolvers();
const results = [];
let cursor = 0;
function next() {
if (cursor < args.length) {
const index = cursor++;
void callback(...args[index]).then(value => {
/**
* @param {number[]} temperatures
* @return {number[]}
*/
var dailyTemperatures = function (temp) {
const result = new Array(temp.length).fill(0);
let stack = [];
for (let i = temp.length - 1; i >= 0; i--) {
while (stack.length && temp[stack[stack.length - 1]] <= temp[i]) stack.pop();
if (stack.length === 0) result[i] = 0;
var nextGreaterElements = function (nums) {
let result = new Array(nums.length);
let stack = [];
const n = nums.length;
for (let i = 2 * n - 1; i >= 0; i--) {
const idx = i % n;
while (stack.length && stack.at(-1) <= nums[idx]) stack.pop();
if (!stack.length) result[idx] = -1;
@hassam7
hassam7 / upper bound binary search.js
Created October 10, 2023 22:43
upper bound binary search in javascript
function upperBound(arr, x) {
let low = 0, high = arr.length - 1;
let ans = arr.length;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
// maybe an answer
if (arr[mid] > x) {
ans = mid;
//look for smaller index on the left
@hassam7
hassam7 / uf.js
Last active September 14, 2024 12:05
Union Find basic javascript
class UnionFind {
constructor(n) {
this.parent = [];
this.size = [];
this.rank = [];
for (let i = 0; i < n; i++) {
this.parent.push(i);
this.size.push(1);
this.rank.push(0);
}
@hassam7
hassam7 / grokking_to_leetcode.md
Created August 19, 2023 22:03 — forked from tykurtz/grokking_to_leetcode.md
Grokking the coding interview equivalent leetcode problems

GROKKING NOTES

I liked the way Grokking the coding interview organized problems into learnable patterns. However, the course is expensive and the majority of the time the problems are copy-pasted from leetcode. As the explanations on leetcode are usually just as good, the course really boils down to being a glorified curated list of leetcode problems.

So below I made a list of leetcode problems that are as close to grokking problems as possible.

Pattern: Sliding Window

@hassam7
hassam7 / n-dimension.js
Created July 18, 2023 11:49
Multi Dimensional Array
const nArray = (rows, cols, defaultValue = undefined) => {
if (rows == 1) return new Array(cols).fill(defaultValue);
else {
let result = new Array(cols);
for (let i = 0; i < cols; i++) {
result[i] = nArray(rows - 1, cols);
}
return result
}
}