# 95

Web Development Engineer interview questions shared by candidates

## Top Interview Questions

Sort: Relevance|Popular|Date
Web Development Engineer was asked...February 25, 2017

### Implement Sum(3)(4)(5)=12 with javascript

const Sum = a =&gt; b =&gt; c =&gt; a + b + c;

``` // Lexically nested function definitions defined within enclosing function function Sum(arg0) { function Inner1(arg1) { function Inner2(arg2) { return arg0 + arg1 + arg2; } return Inner2; } return Inner1; } console.log(Sum(3)(4)(5)); ``` Less

var sum=0; // global variable function Sum(num){ sum=num+sum; return Sum; } Sum(3)(4)(5); Less

### Vertically and horizontally center an element on the screen using css.

#center{width: 100px; height: 50px; position: fixed; top: 0; left: 0; right: 0; bottom: 0; margin: auto;} Less

wrong answer. u need to also margin it negative

. container{ Position : absolute; Width : 200px; Top : 50℅; Left : 50℅; Transform : translate(-50℅) } Less

### Take two arrays and compare them to find duplicates. Only list each duplicate once.

The most upvoted answer on this has a run time complexity of O(m * n) where m and n are the arrays. This is solvable in O(n) by using a hash map, by sacrificing space for complexity: var Array1 = ["a", "b", "c", "d", "e", "f", "c"]; var Array2 = ["c", "x", "y", "f", "c"]; let hash = {}; let result = []; // loop over the largest for (let i = 0; i &lt; Array1.length; i++) { if (!hash[Array1[i]]) { hash[Array1[i]] = 1; } } for (let i = 0; i &lt; Array2.length; i++) { if (hash[Array2[i]]) { result.push(Array2[i]); delete hash[Array2[i]]; } } console.log(result); Less

var Array1 = ["a", "b", "c", "d", "e", "f"]; var Array2 = ["c", "x", "y", "f"]; Array1 = Array1.filter(function(val) { return Array2.indexOf(val) !== -1; }); Less

I think that the last solution suggested might still return duplicates. For example if array1 = [1,2,3] and array2 = [2,2,2] we will get the result of [2,2]. This is another solution in O(n) - function findDuplicates(arr1, arr2) { var dict = {}; var duplicates = {}; for (i = 0; i &lt; arr1.length; i++ ) { dict[arr1[i]] = 1; } for (i = 0; i &lt; arr2.length; i++ ) { if (dict[arr2[i]]) { duplicates[arr2[i]] = arr2[i]; } } return Object.values(duplicates); } Less

### How would you implement integer division if your language did not offer it.

#Assumes positive numbers def divide(num, divide_by) answer = 0 return answer if divide_by == 0 while(num &gt;= divide_by) num = num - divide_by answer = answer + 1 end answer end puts divide(10,0) Less

Simpler version (assuming you are allowed to use multiplication), just compute the sign at the end and multiply: function divide(a, b){ if(b == 0) throw "Cannot divide by zero"; var remainder = Math.abs(a); var dividend = Math.abs(b); var result = 0; while(remainder &gt;= dividend){ result++; remainder -= dividend; } if(result &gt; 0 &amp;&amp; a*b &lt; 0) result *= -1; return result; } Less

http://www.bearcave.com/software/divide.htm

### How to discover if the given number is Power of 2 ?

return (( x!=0) &amp;&amp; (x &amp; (x -1) == 0)) a number which is power of two will have single 1 in binary representation ex. 16 00010000 x-1 will have pattern 00001111 hence x&amp;(x-1) will always be 0 for power of two numbers. Need to check condition for x!=0 since 0 is not a power of two. Special case to handle. Less

if you can figure this out inside an hour, congratulations this isnt something that should be in an interview to be honest the level of difficulty of the answer that algo came up with is at the level of an upper division course that has a proof of it written in wikipedia i honestly like the brute force solution Less

Divide the number by 2, and repeat, until the result is a not an integer number (in this case is not a power of 2) or the result is 1 (in this case the number was a power of 2). Less

### Print the number of occurrences of a number in a sorted array of numbers. Input: 1 2 3 4 4 4 4 4 5 6 7 | 4 Ouput: 5

const counter = (array, num) =&gt; { let a = numbers.indexOf(num); if (a &gt;= 0) return numbers.lastIndexOf(num) - a + 1; return 0; } Less

how to execute this code const counter = (array, num) =&gt; { let a = numbers.indexOf(num); if (a &gt;= 0) return numbers.lastIndexOf(num) - a + 1; return 0; } Less

const sort = (array, target) =&gt; { return array.filter(item =&gt; item==target).length; } Less

### Make an accordion where when clicked, text expands from it and when another item is clicked, the first one collapses and the second one expands.

script - var accordion = document.getElementsByClassName("accordion"); var i; for (i=0; i Less

HTML - <div> <div>element number 1</div> <div>element number 2</div> <div>element number 3</div> <div>element number 4</div> <div>element number 5</div> </div> CSS - .accordion-element { display: inline-block; overflow: hidden; width: 20px; height: 20px; } .accordion-element.expanded { width: auto; } Javascript - function expandElement(elem) { var elementsToRemoveClassFrom = document.getElementsByClassName('expanded'); for (var i = 0; i &lt; elementsToRemoveClassFrom.length; i++ ) { elementsToRemoveClassFrom[i].classList.remove('expanded') } elem.classList.add('expanded') } Less

My answer was shortened by Glassdoor security mechanism. What's missing from my last answer is that for each HTML DIV element I added an "oncl1ck" attribute which called "expandElement(this)" Less

### filter table using vanilla js

Assuming it's a simple "equal" filter and the table is a 2-dimensional array like this- var table = [ ['name', 'occupation', 'age'], ['Roy1', 'Software Engineer', '29'], ['Roy2', 'Software Engineer', '26'] ]; I'd go with - function filterTable(table, param, value) { var paramIndex = table.indexOf(param); return table.reduce(function (acc, row) { if (row[paramIndex] === value) { acc.push(row); } return acc; }, []) } And then, for example for the input - filterTable(table, 'age', '29') I'd get back - [['Roy1', 'Software Engineer', '29']] Less

var table = [ ['name', 'occupation', 'age'], ['Roy1', 'Software Engineer', '28'], ['Roy2', 'Software Engineer', '29'] ]; function filterTable(table, param, value) { var paramIndex = table.indexOf(param); return table.filter(item=&gt;item[paramIndex]===value) } var res=filterTable(table, 'age', '29') console.log(abc) Less

var table = [ ['name', 'occupation', 'age'], ['Roy1', 'Software Engineer', '29'], ['Roy2', 'Software Engineer', '26'] ]; let filterTable = (table, field, val) =&gt; { const col = table.indexOf(field); return table.filter(row =&gt; row[col] === val); } let res = filterTable(table, 'name', 'Roy2'); console.log(res); Less

### How would you reverse a linked list?

For a singly-linked list consiting of n linked nodes... i -2) { node[i+2].next &lt;-- &amp;node[i+1]; i = i - 1; } node.next &lt;-- null; Less

dave, the tricky answer given below is not acceptable in interview as well as in real programming. Please dont take these words as negative but try formulating solutions which are simple to understand and elegant. for example the invariants in above code and termination condition of -2 is not good. for example you can write -- Node* prev = &amp;head; Node* curr = prev-&gt;next; while(curr) { Node* tmp = curr-&gt;next; curr-&gt;next = prev; prev=curr; curr = tmp; } return prev; Less