DP World Frontend Interview Experience

Position: Group SDE - II (Frontend) Round 2

I recently interviewed with DP World, and here is the information I can provide.

  1. Flatten an array
const arr = [1,2,[1,1,[34,67,5,1,1,1,[{a: 1}]]]];
const b = [[1,2],0,0,0,[3,[4,5]],[6,7,[[[8],9]]],10];
let newArr = [];

const flatten = (arr) => {
  let len = arr.length;
  for (let i=0; i<len; i++) {
    if(!Array.isArray(arr[i])) 
      {
        newArr.push(arr[i]);
      } else {
      flatten(arr[i]);
    }
  }
}
console.log(flatten([1, 2, 3]));
console.log(newArr);
  1. Provide maximum solutions to form sum from an array with the given target.

     const numbers = [2,3,6,7];
     const target = 7;
    
     function combinationSum(num, target) {
         const result = [];
    
         function backtrack(start, target, path) {
             if (target === 0) {
                 result.push([...path]);
                 return;
             }
    
             for (let i = start; i < num.length; i++) {
                 if (num[i] <= target) {
                     path.push(num[i]);
                     backtrack(i, target - num[i], path);
                     path.pop();
                 }
             }
         }
    
         num.sort((a, b) => a - b); // Sort the numbers
         backtrack(0, target, []); // Start backtracking from the beginning
         return result;
     }
    
     const numbers = [1, 2, 3, 6, 7];
     const target = 7;
     console.log(combinationSum(numbers, target));
    
     /* A solution set is:
     [
       [7],
       [2,2,3]
     ] */
    
    1. How to achieve Privatisation of variables through Javascript?

    2. Explain Function Currying.

    3. Output Question:

       import React, { useState } from "react";
       import { screen } from "@testing-library/dom";
       import userEvent from "@testing-library/user-event";
      
       const A =() => {
         console.log("render A");
         return null;
       }
      
       const App =() => {
         const [_state, setState] = useState(false);
         console.log("render App");
         return (
           <div>
             <button
               onClick={() => {
                 console.log("click");
                 setState(true);
               }}
             >
               click me
             </button>
             <A />
           </div>
         );
       }
      
       userEvent.click(screen.getByText("click me"));
       userEvent.click(screen.getByText("click me"));
       userEvent.click(screen.getByText("click me"));
      
       /*
       click
       render App
       render A
       click
       render App
       render A
       click
       render App
       render A
      
       1. The initial rendering occurs when the App component is first rendered. 
       Both "render App" and "render A" are logged to the console.
       2. When the button with the text "click me" is clicked for the first time, 
       the click event handler sets the state to true, triggering a re-render of the App component. 
       This causes "click" and "render App" to be logged to the console again.
       3. Since the state has changed, React also re-renders the A component, resulting in "render A" 
       being logged again.
       The same process repeats for the second and third clicks
       on the button, resulting in additional "click" and "render" logs.
       */
      

      Apart from this, there were several questions prototypes and data hiding.

      Thank you for Reading!