Skip to content

Instantly share code, notes, and snippets.

View carefree-ladka's full-sized avatar
🏠
Working from home

Pawan Kumar carefree-ladka

🏠
Working from home
View GitHub Profile
@carefree-ladka
carefree-ladka / InfiniteScroller.js
Created June 30, 2024 13:12
Infinite Scrolling in React.js
import React, { useState, useEffect } from 'react';
const InfiniteScroll = () => {
const [data, setData] = useState([]);
const [page, setPage] = useState(1); // Track current page
const [loading, setLoading] = useState(false); // Track loading state
const fetchData = async () => {
setLoading(true); // Set loading state
try {
const inorderTraversal=function(root){
let node = root;
const result=[]
while (node) {
if (!node.left) {
result.push(node.val);
node = node.right;
} else {
const predecessor = findPredecessor(node);
if (predecessor.right === node) {
@carefree-ladka
carefree-ladka / QuickSelect.js
Created June 11, 2024 14:16
Quick Select Algorithm for finding Kth Max Element
function partition(a, lo, hi) {
/*
//Improve partition choice for the worst case
const randomIndex = Math.floor(Math.random() * (hi - lo + 1)) + lo;
swap(a, lo, randomIndex); // Move the random pivot to the start
*/
let i = lo;
let j = hi + 1;
let P = a[lo];
@carefree-ladka
carefree-ladka / Twitter.js
Created May 22, 2024 15:48
Twitter Like System
class Twitter {
constructor() {
this.users = {};
this.tweets = [];
this.followers = {};
}
postTweet(userId, content) {
const tweet = {
id: this.tweets.length,
@carefree-ladka
carefree-ladka / NryTree.js
Created May 18, 2024 12:39
Nry Tree Implementation
class Node {
constructor(val) {
this.val = val;
this.children = [];
}
addChild = (child) => this.children.push(child)
}
@carefree-ladka
carefree-ladka / Flatten.js
Created May 17, 2024 12:23
Object flatten in JS
const getAllKeys = (obj) => {
const result = [];
const queue = [{ path: [], obj }];
while (queue.length) {
const { path, obj: currentObj } = queue.shift();
for (const [key, value] of Object.entries(currentObj)) {
const newPath = path.concat(key);
if (typeof value === 'object' && value !== null) {
queue.push({ path: newPath, obj: value });
@carefree-ladka
carefree-ladka / QuickSelect.js
Created May 13, 2024 15:06
QUICK SELECT ALGORITHM
const quickSelect = (nums, k, kthMax) => {
if (!nums.length) return;
const P = nums[Math.floor(Math.random() * nums.length)];
const left = [];
const mid = [];
const right = [];
nums.forEach(x => {
@carefree-ladka
carefree-ladka / Trie.js
Last active May 7, 2024 04:31
Trie implementation in JavaScript with Autocomplete feature
class TrieNode {
constructor() {
this.children = {}
this.eow = false
this.word = ""
}
}
class Trie {
class Node {
constructor(val = 0, left = null, right = null) {
this.val = val;
this.left = left;
this.right = right;
}
}
const root = new Node(6)
root.left = new Node(3)
@carefree-ladka
carefree-ladka / pokemon.js
Created May 5, 2024 03:41
List all pokemons by based on their gender
//https://pokeapi.co/api/v2/pokemon?limit=100
//https://pokeapi.co/api/v2/gender/1
//https://pokeapi.co/api/v2/pokemon/1000
const fetcher = async (url) => {
try {
const res = await fetch(url)
if (res.ok || res.status === 200) return await res.json()
}
catch (e) {