# PUPPALA HARIKA | 4+ yrs exp | R2 - - Nodejs, Javascript - MySQL, Redis - AWS: Kafka, S3, IAM, RDS, KMS # Question 1: Js problem ```javascript! const nestedObj = { a: "Hi", b: { c: "Hello", g: {h: "World"}}, d: { e: { f: 2} } } // const flattenObj = { // a: "Hi", // b_c: "Hello", // b_g_h: "World", // d_e_f: 2 // } function flattenObject(nestedObj){ const res ={}; for(const i in nestedObj){ if((typeof nestedObj[i]) ==="object" && nestedObj[i]!== null){ const flatObj= flattenObject(nestedObj[i]); for(const j in flatObj){ if(!flatObj.hasOwnProperty(j)) continue; res[i+'_'+j] = flatObj[j]; } }else{ res[i] = nestedObj[i]; } } return res; } const result = flattenObject(nestedObj); console.log(result); ``` # Question 2: - Array [1, 2, 3, 4, 5] (distinct) k = 3 , 0<k<array.length [1, 2, 3] [1, 2, 4] [1, 2, 5] [1, 3, 4] [1, 3, 5] .... ```javascript! function combinations(arr, k){ let result =[] function getCombinations(start, curr){ if(curr.length===k){ result.push([...curr]); return } let prev = null; for(let i=start;i<arr.length;i++){ if(prev!== arr[i]){ curr.push(arr[i]); getCombinations(i+1, curr); curr.pop(); prev = arr[i]; } } } arr.sort((a,b)=> a-b); getCombinations(0,[]); return result; } let arr = [1, 2, 3, 4, 5]; console.log(combinations(arr, 3)); ``` # Question 3: Kafka Usecases - Throughput - Retention Kafka ------ Topics Producer() consumer() Topic: Will take a array/queue and when new messages arrive , we will append to queue. Producer: Class KafkaProducer{ constructor(){ this.topics={}; } createTopic(topicName){ if(!this.topics[topicName]){ this.topics[topicName] =[] } } produce(topicName, message){ if(!this.topics[topicName]){ console.warn("topic is not prent , create ") this.createTopic(topicName) } // push messages to that topic this.topics[topicName].push(message); } printMessage(topicName){ // put some logic to print it } } Consumer : 1. subscribe 2. consume Class KafkaConsumer{ constructor(kafka, groupId){ this.kafka = kafka; this.groupId = groupId; this.offset = {}; } subscribe(topicName){ if(!this.offset[topicName]){ this.offset[topicName]=0; } } consume(topicName){ if(!this.kafka.topics[topicName]){ console.warn("topic is not prent , create ") return; } const messages = this.kafka.topics[topicName] const newMessage = messages.slice(this.offset[topicName]); if(newMessage.length===0){ console.log("no messages in the ${topicName } for this groupig") return } // updating offset to latest this.offset[topicName] += newMessage.length } } const kafka = new KafkaProducer(); kafka.createTopic("topic1"); kafka.produce("topic1", {id:1, text : "Hi sample message"}); const consumerA = new KafkaConsumer(); consumerA # Question 4: 25 Horses Fastest 3 horse. 5 horse in a race. 1. lets assume 5 races 2. race between winners of each grp against each other = winner is the fastest of all 3. try to discard remaining : all horses in the grp where overall fastest is present , exccept the top two . 4. try to discard remaining: bottom 2 horses of grp that second fastest is present . 5. try to discard remaining:all horses in the grp where 3 fastest is present, except for 2 place. 6. try to discard remaining: all horses the fourth and fifth placed grps top 2 of fastest, winner from second and third, second place from third = 5 horses 7. race these 5 horses , you will toable to get first , second, third ranked horses. A,B,C,D,E A1> 2 B1> (A1,A2,B1) C1> (A1,A2,B1) D1 E1 A1, A2, B1, B2, C1, C2 B2< A2 (B1<A1) A2, B1, B2, C1, C2 - RACE A2> B1> C1> B2>C2 - RESULT