Mostly asked Data Structure questions for full stack developer(both front-end and back-end) position.
DS and Algos
Data structures and algorithms are always been important for developers like us to keep our knowledge updated. There is always been an elimination round for giant companies like Amazon, Facebook, Google, etc.
Today I am hereby sharing three questions of DS and algorithms and in the future will update this article with some more.
So now without getting bored with the intro, let’s get dive into questions.
NOTE: The solutions below are entirely given in Javascript. If you want, you can directly copy-paste them in your browser’s developer console(F12 or fn + F12)(in the CONSOLE tab) and test them out.
Problem Statement
Given a two-dimensional array, if any element within is zero, make its whole row and column zero.
Solution:
let make_zeroes = function(matrix) {
if (!matrix || matrix.length === 0) {
return;
}
let zero_rows = new Set();
let zero_cols = new Set();
let rows = matrix.length;
let cols = matrix[0].length;
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (matrix[i][j] === 0) {
if (!zero_rows.has(i)) {
zero_rows.add(i);
}
if (!zero_cols.has(j)) {
zero_cols.add(j);
}
}
}
}
zero_rows.forEach(function(r) {
for (let c = 0; c < cols; c++) {
matrix[r][c] = 0;
}
});
zero_cols.forEach(function(c) {
for (let r = 0; r < rows; r++) {
matrix[r][c] = 0;
}
});
console.log(matrix)
}make_zeroes([[1,2,3,4,5],[1,2,2,0,0],[0,1,1,1,2],[1,2,3,4,5],[1,4,1,5,2]])
Problem Statement
Given an array nums
, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.
For example, given nums = [0, 1, 0, 3, 12]
, after calling your function, nums
should be [1, 3, 12, 0, 0]
.
Solution:
var moveZeroes = function(nums) {
let count = 0;
for (let i = 0; i < nums.length; i++){
if(nums[i] !== 0){
nums[count++] = nums[i];
}
} for (let i = count; i < nums.length; i++){
nums[i] = 0;
}
return nums;
};
Problem Statement
Check if parenthesis in a string is balanced or not:
Solution:
var isValid = function (s) { const obj = {
"(": ")",
"{": "}",
"[": "]",
} const stack = []; for (const paran of s) {
if (obj.hasOwnProperty(paran)) {
stack.push(paran)
} else {
const closeParan = stack.pop();
if (paran !== obj[closeParan]) {
return false;
}
}
} return stack.length === 0;
};
PROBLEM STATEMENT:
Design snake ladder board in react.
Solution:
App.js
import "./styles.css";const boardConfig = {start: 1,end: 100,rows: 10,cols: 10,steps: {21: 53,28: 48,36: 67,42: 9,45: 12,51: 78,57: 33,59: 82}};const renderRow = (ind) => {const arr = new Array(boardConfig.cols);for (let index = 0; index < boardConfig.cols; index++) {arr.push(<div>{1 + index + ind * 10}</div>);}return arr;};const renderAllRows = () => {const arr = new Array(boardConfig.cols);for (let index = boardConfig.cols - 1; index >= 0; index--) {arr.push(<>{index % 2 === 0 && <div className="row">{renderRow(index)}</div>}{index % 2 !== 0 && (<div className="reverse-row">{renderRow(index)}</div>)}</>);}return arr;};const Board = (props) => {return <div>{renderAllRows()}</div>;};export default function App() {return (<div className="App"><h1>Hello CodeSandbox</h1><Board config={boardConfig} /></div>);}
Index.js
import { StrictMode } from "react";import ReactDOM from "react-dom";import App from "./App";const rootElement = document.getElementById("root");ReactDOM.render(<StrictMode><App /></StrictMode>,rootElement);
styles.css
.App {font-family: sans-serif;text-align: center;}.row {display: flex;justify-content: space-around;}.reverse-row {display: flex;justify-content: space-around;flex-direction: row-reverse;}
PROBLEM STATEMENT:
Search an element in a sorted and rotated array.
SOLUTION:
function binarySearch( arr, low, high, key){ if (high < low) return -1; let mid = Math.floor((low + high) / 2); /*low + (high - low)/2;*/ if (key == arr[mid]) return mid; if (key > arr[mid]) return binarySearch(arr, (mid + 1), high, key); return binarySearch(arr, low, (mid - 1), key);}function findPivot( arr, low, high){ if (high < low) return -1; if (high == low) return low; let mid = Math.floor((low + high) / 2); /*low + (high - low)/2;*/ if (mid < high && arr[mid] > arr[mid + 1]) return mid; if (mid > low && arr[mid] < arr[mid - 1]) return (mid - 1); if (arr[low] >= arr[mid]) return findPivot(arr, low, mid - 1); return findPivot(arr, mid + 1, high);}function pivotedBinarySearch( arr, n, key){ let pivot = findPivot(arr, 0, n - 1); if (pivot == -1) return binarySearch(arr, 0, n - 1, key); if (arr[pivot] == key) return pivot; if (arr[0] <= key) return binarySearch(arr, 0, pivot - 1, key); return binarySearch(arr, pivot + 1, n - 1, key);}let arr1 = [ 5, 6, 7, 8, 9, 10, 1, 2, 3 ];let n = arr1.length;let key = 3;// Function callingconsole.log( "Index of the element is : "+ pivotedBinarySearch(arr1, n, key));
PROBLEM STATEMENT:
Poly-fill of Promises in javascript.
SOLUTION:
As per MDN web docs, Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value. A promise can be in three states Pending, Fulfilled or Rejected. It provides us methods like then and catch to pass in the appropriate handlers.
Promise.allSettled Pollyfill
Promise.allSettled = function (promises) {
let mappedPromises = promises.map((p) => {
return p
.then((value) => {
return {
status: ‘fulfilled’,
value,
};
})
.catch((reason) => {
return {
status: ‘rejected’,
reason,
};
});
});
return Promise.all(mappedPromises);
};
Promise.all polyfill —
Debounce and throttle functionality for FE development -
https://webdesign.tutsplus.com/tutorials/javascript-debounce-and-throttle--cms-36783
Write a program to calculate pow(x,n)
function power(x, y)
{
var temp;
if (y == 0)
return 1;
temp = power(x, parseInt(y / 2));
if (y % 2 == 0)
return temp * temp;
else
{
if (y > 0)
return x * temp * temp;
else
return (temp * temp) / x;
}
}
Write a modular FE code for SEARCHING text in the textbox
HW.(https://medium.com/nerd-for-tech/debounce-your-search-react-input-optimization-fd270a8042b)
Write a modular FE code for a working carousel in the browser.
HW.(https://codesandbox.io/s/elastic-hill-cmbz1?from-embed=&file=/src/App.js)
Write a modular FE code for a working directory collapse and expand
HW.(https://dev.to/michaelburrows/build-a-custom-react-accordion-component-1nep, https://codesandbox.io/s/zealous-meadow-om7kuo?file=/src/data.js:0-1127, https://codesandbox.io/s/bold-rubin-p0qvo?file=/src/App.js)
Design chess game in frontend.