# Sameer Pathan | >5 yrs - Good with coding - Gave answer in write direction for system design problem - Can proceed with next round - Nodejs, Java - PSQL, Mongo - RabbitMQ - AWS (s3, EC2, lambda) - Kubernetics # Question 1: - Array - 0 < l < Array.length() [1, 2, 3, 4, 5] l = 3 [1, 2, 3] [1, 2, 4] [1, 2, 5] [1, 3, 4] [1, 3, 5] [2, 3, 5] ```javascript! function permutate(len, currArray, visited, inp, start){ if(currArray.length == len){ console.log(currArray) // let lastVal = currArray[currArray.length - 1]; // let ind = currArray.indexOf(lastVal); // visited[ind] = false; currArray.pop(); return; } for(let i = start; i < inp.length; i++){ if(!visited[i]){ visited[i] = true; currArray.push(inp[i]); permutate(len, currArray, visited, inp, start); } } } inp = [1,2,3,4,5]; visited = []; currArray = [] for(let i = 0; i < 5; i++) visited[i] = false; for(let i = 0; i < 5; i++){ currArray = [] permutate(3,currArray,visited,inp, i); for(let i = 0; i < 5; i++) visited[i] = false; } ``` # Question 2: High number clicks events -> APIs APIs -> Stored Integrated in the front end -> react sdk for consumption from the client event -> { types (of n types) } queuing mechanism -> the event is pushed to the queue -> consumer which consumes from the queue (lambda function listening on the queue) -> consumer -> to process the event publish these results either directly to a db or to lets a kafka topic multiple topics base don type of event (drop off from payment page high prioirty event) scheculed lambda function -> looks at the queue every x mins and takes the events, batches it and pushed it to the data store -> service ( consists pods ec2 infrastructure) # Question 3: Url Shortner Service. HLD -> given a url, shorten it https://hackmd.io/Pfm9ykv9RSKI8p4TNlWO7w?edit -> https://bityly.in/r/abcxyz service (with api exposed for shorten) /shorten take request -> -> extract original url from the request -> apply shortening (key) logic -> save to the db (nosql -> mongodb, redis) -> return the shortened url performance Infrastructure -> (AWS ECS with fargate to manage container orchestration or k8s with EKS (elastic kubernetes service)) Database -> Managed MongoDB (Gives us horizontal scaling) with master/slave (replica sets) writes on master and reads on replicas Load balancer -> Application load balancer Additional performance -> Redis storing key value pairs (Good performance for high reads) A celebeity posts a shortened url on twitter -> resolutions high /resolve (this will depend on the scale) redirect the user to entry (in the cache) Front end -> extracts the unique hash key, makes the api call which in turn redirects to the mapped url stored in cache/DB APIs -> POST /shorten GET /resolve POST /shorten payload: body: { url: "https://google.com" } GET /resolve queryParam: { url: "https://bityly.in/r/abc" } validation -> length of url (since shortening length will be fixed) collision handling -> query the cache and see if key already exists (ensures very low collision) Database -> Since its a noSQL DB -> strict schema is not required { _id: "uniqueId" originalUrl: "http://google.com" shortenedUrl: "bitly.in/r/abc" } Cache -> { key: value } example: { "bitly.in/r/abc": "http://google.com" } Key generation -> uuid generator or any available generator Analytics: key metrics domains which are getting shortened frequency against a domain mapping in a time window Event -> types -> shorten, resolve -> payload{ type: "shorten" url: "google.com" timestamp: "21/09/2023" } analytics events can be triggered in async (fire and forget) Kafka / RabbitMQ makes sense Pub/Sub system Service is a publisher publishing analytics events Consumer -> Node process or a lambda function listening on a topic, after receiving an event, process and perisist to a DB topic (NOSQL solution) Prevention from DDOS -> Generate user1 -> woould require authentication using this uid, it generates 1000 requests in last 10 seconds, so deny the service to the suer t1 t1+1 t1+2 t1+3 -> sliding window for getting information about requests rate limiting