Skip to content

Instantly share code, notes, and snippets.

@AbeEstrada
AbeEstrada / longestWord.ts
Created September 17, 2025 00:18
Exercise: find the longest word in a sentence
const longestWord = (str: string): void => {
if (str?.length === 0) return;
const words: string[] = str.split(" ");
const longest: string = words.reduce(
(a, b) => (b.length > a.length ? b : a),
"",
);
console.log(longest.length, longest);
};
@AbeEstrada
AbeEstrada / commits.md
Created September 13, 2025 20:38
Conventional Commits
build(build system or external dependencies changes)
ci(CI configurations and scripts changes)
docs(documentation)
feat(feature)
fix(bug fix)
perf(improves performance)
refactor(restructuring existing code, but not bug or feature)
revert(reverts a previous commit)
style(formatting, missing semi colons, etc.)
@AbeEstrada
AbeEstrada / whatFlavors.js
Created September 3, 2025 03:26
Exercise: Hash Tables Ice Cream Parlor
function whatFlavors(cost, targetAmount) {
const priceToIndexMap = new Map();
for (const [currentIndex, currentPrice] of cost.entries()) {
const neededPrice = targetAmount - currentPrice;
const displayIndex = currentIndex + 1;
if (priceToIndexMap.has(neededPrice)) {
const firstFlavorIndex = priceToIndexMap.get(neededPrice);
console.log(`${firstFlavorIndex} ${displayIndex}`);
return;
}
@AbeEstrada
AbeEstrada / checkMagazine.js
Created September 3, 2025 03:18
Exercise: Hash Tables Ransom Note
function checkMagazine(magazine, note) {
// Create a frequency map of words available in the magazine
const availableWords = new Map();
for (const word of magazine) {
const currentCount = availableWords.get(word) ?? 0;
availableWords.set(word, currentCount + 1);
}
// Check if we can form the note using available words
for (const requiredWord of note) {
const remainingCount = availableWords.get(requiredWord) ?? 0;
@AbeEstrada
AbeEstrada / quickSort.js
Created September 3, 2025 00:55
Exercise: Quicksort (Partition)
function quickSort(arr) {
if (arr.length === 0) return [];
const pivot = arr[0];
const left = arr.filter((num, i) => i !== 0 && num < pivot);
const equal = arr.filter(num => num === pivot);
const right = arr.filter((num, i) => i !== 0 && num > pivot);
return [...left, ...equal, ...right];
}
@AbeEstrada
AbeEstrada / bigSorting.js
Created September 3, 2025 00:45
Exercise: Big Sorting
function bigSorting(unsorted) {
return unsorted.sort((a,b) => {
const bigA = BigInt(a);
const bigB = BigInt(b);
if (bigA < bigB) return -1;
if (bigA > bigB) return 1;
return 0;
});
}
@AbeEstrada
AbeEstrada / checkLevelOrderBST.js
Created September 3, 2025 00:27
Exercise: Is This a Binary Search Tree?
function checkLevelOrderBST(levelOrder) {
if (levelOrder.length === 0) return true;
let i = 0;
const n = levelOrder.length;
// Stack to store nodes with their min and max constraints
const stack = [];
stack.push({
data: levelOrder[0],
min: -Infinity,
max: Infinity,
@AbeEstrada
AbeEstrada / bstInsertion.js
Last active September 3, 2025 00:19
Exercise: Binary Search Tree Insertion
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
function insert(root, data) {
if (root === null) {
return new Node(data);
@AbeEstrada
AbeEstrada / bfs.js
Created September 3, 2025 00:05
Exercise: Breadth First Search Shortest Reach
function bfs(n, m, edges, s) {
// Create adjacency list for the graph
const adjList = new Array(n + 1).fill().map(() => []);
// Build the graph from edges
for (const [u, v] of edges) {
adjList[u].push(v);
adjList[v].push(u); // Undirected graph
}
// Initialize distances array with -1 (unreachable)
const distances = new Array(n + 1).fill(-1);
@AbeEstrada
AbeEstrada / diagonalDifference.js
Created September 2, 2025 23:54
Exercise: Diagonal Difference
function diagonalDifference(arr) {
let left = 0;
let right = 0;
const n = arr.length;
for (let i = 0; i < n; i++) {
left += arr[i][i];
right += arr[i][n - i - 1];
}
return Math.abs(left - right);
}