Posts

Showing posts with the label Java Interview Questions

JavaScript interview questions & answers with code

Image
Top JavaScript Interview Questions and Expert Answers with Code Examples 1. Flatten a Nested Object Question : Write a function to flatten a nested JavaScript object. Code : function flattenObject(obj, prefix = '', res = {}) {   for (let key in obj) {     const newKey = prefix ? `${prefix}.${key}` : key;     if (typeof obj[key] === 'object' && obj[key] !== null) {       flattenObject(obj[key], newKey, res);     } else {       res[newKey] = obj[key];     }   }   return res; } console.log(flattenObject({ a: { b: { c: 1 } }, d: 2 })); // Output: { 'a.b.c': 1, d: 2 } 2. Find Duplicates in an Array Question : Write a function to find duplicate values in an array. Code : function findDuplicates(arr) {   const counts = {};   return arr.filter(item => counts[item] ? true : (counts[item] = 1, false)); } console.log(findDuplicates([1, 2, 2, 3, 4, 4, 5])); // Output: [2, 4] 3. Implement deboun...

Javascript interview questions and answers with code

Image
To start a series for the JavaScript interview questions which may help junior to mid level developers. Common JavaScript interview questions with examples. 1. What is let and const in JavaScript? »  Let and const are block-scoped declarations in JavaScript, used to declare variables. let allows you to reassign the value of the variable, while const creates a read-only reference to a value. Example: let message = "Hello, World!"; message = "Hello, JavaScript!"; console.log(message); // Output: "Hello, JavaScript!" const PI = 3.14; PI = 3.14159; // TypeError: Assignment to constant variable. 2. What is arrow function in JavaScript? »   Arrow functions are a shorthand for anonymous functions in JavaScript. They are also known as “fat arrow” functions. Example: let add = (a, b) => a + b; console.log(add(1, 2)); // Output: 3 let numbers = [1, 2, 3, 4, 5]; let doubledNumbers = numbers.map(num => num * 2); console.log(doubledNumbers); // Output: [2, 4, 6, ...