avatarVasanth Bhat

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2271

Abstract

lockquote><div id="6cb8"><pre><span class="hljs-built_in">let</span> arr = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,[<span class="hljs-number">3</span>,<span class="hljs-number">4</span>]]; <span class="hljs-built_in">let</span> <span class="hljs-built_in">last</span> = arr.<span class="hljs-built_in">pop</span>(); // Ex: [<span class="hljs-number">3</span>,<span class="hljs-number">4</span>] arr.<span class="hljs-built_in">push</span>(...<span class="hljs-built_in">last</span>); // Spread operator will <span class="hljs-built_in">convert</span> [<span class="hljs-number">3</span>,<span class="hljs-number">4</span>] to <span class="hljs-number">3</span>,<span class="hljs-number">4</span> console.<span class="hljs-built_in">log</span>(arr)</pre></div><p id="0699">Copy this code and run, you should get an insight about the crux of the logic. Below explains it in detail.</p><p id="fb03"><b>Algorithm</b></p><ol><li>Taking the last element of the nested array,</li><li>In case if it is also an array then we use spread operator to eliminate the array and push it back to array.</li><li>If it is not an array then don’t use spread operator just push the value into array.</li></ol><blockquote id="7d19"><p><b>Complete code</b></p></blockquote><div id="8c83"><pre>function flatten(<span class="hljs-keyword">input</span>) { <span class="hljs-keyword">const</span> <span class="hljs-keyword">stack</span> = [...<span class="hljs-keyword">input</span>]; <span class="hljs-keyword">const</span> res = []; <span class="hljs-keyword">while</span> (<span class="hljs-keyword">stack</span>.length) { <span class="hljs-comment">// pop value from stack</span> <span class="hljs-keyword">const</span> next = <span class="hljs-keyword">stack</span>.pop(); <span class="hljs-keyword">if</span> (Array.isArray(next)) { <span class="hljs-comment">// push back array items, won't modify the original input</span> <span class="hljs-keyword">stack</span>.push(...next); } <span class="hljs-keyword">else</span> { res.push(next); } } <span class="hljs-comment">// reverse to restore input order</span> <span class="hljs-keyword">return</span> res.<span class="hljs-built_in">reverse</span>(); }

<span

Options

class="hljs-keyword">const</span> arr = [1, 2, [3, 4, [5, 6]]]; flatten(arr);</pre></div><p id="c929"><b>Explanation:</b></p><div id="c15f"><pre>const stack = [...input]; // it is just a deep copy of variable. Otherwise source<span class="hljs-built_in"> array </span>will // be modified</pre></div><p id="62f5"><b>res</b> variable is used to hold the final result.</p><p id="1a26">return value is reversed res, because we are performing our activity from the last. So, while finally sending the result it should be reversed to make it in the actual order.</p><div id="b145"><pre><span class="hljs-keyword">return</span> res.<span class="hljs-keyword">reverse</span>();</pre></div><p id="5fba">If you have not followed me on Medium, please follow. Do not forget to subscribe to my Youtube <a href="https://www.youtube.com/channel/UCSCNvSCk_Z9mBvUM-FJexRg/videos">Channel</a>,</p><p id="543a">In case if you want to talk to me personally for mock interview, tips and tricks to clear interview or resume review, you can book a session here:</p><div id="9932" class="link-block"> <a href="https://topmate.io/vasanth_bhat"> <div> <div> <h2>Book a time with Vasanth on topmate.io</h2> <div><h3>Simplifying personalised interactions for the world's leading minds</h3></div> <div><p>topmate.io</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*DFl_v6cSzu9N-FHE)"></div> </div> </div> </a> </div><p id="b7c7">If your preparing for frontEnd developer interview, please watch below series of mine:</p><p id="dc2d"><a href="https://www.youtube.com/watch?v=qcixpy3HQ9s&amp;list=PLmcRO0ZwQv4QMslGJQg7N8AzaHkC5pJ4t">https://www.youtube.com/watch?v=qcixpy3HQ9s&amp;list=PLmcRO0ZwQv4QMslGJQg7N8AzaHkC5pJ4t</a></p><p id="41ff">If you want to learn JavaScript custom implementations of built in method, then watch below series of mine:</p><p id="ed48"><a href="https://www.youtube.com/watch?v=eGzErMUfdpk&amp;list=PLmcRO0ZwQv4RWqjZCajaKBAdCJjmkAL_I">https://www.youtube.com/watch?v=eGzErMUfdpk&amp;list=PLmcRO0ZwQv4RWqjZCajaKBAdCJjmkAL_I</a></p></article></body>

Solve Meta, Amazon, Google, Apple, Microsoft frontEnd developer interview question

After I’m researching a lot on web, I came to a conclusion that, there is no tutorial/video series that is dedicated for MAANG(Meta, Apple, Amazon, NetFlix, Google) preparation for FrontEnd developers. So, I decided to decode most common interview questions of MAANG in my Youtube channel. In this article I will discuss a very interesting problem. So, read till the end.

Flatten an array without using Recursion.

Question: Flatten the given array

Sample Input: [1,2,[3,4],5]

Sample Output: [1,2,3,4,5]

Company where the question is asked: Meta/Microsoft/Google/Apple/Amazon

I have written recursive solution for this problem here. It is very important to know the recursive approach, as most companies expect you to know this solution. But iterative approach too is not that easy. So, in interview they may ask you to write both the solutions. So, in this article I will be explaining Iterative approach.

Crux of the logic

let arr = [1,2,[3,4]];
let last = arr.pop(); // Ex: [3,4]
arr.push(...last); // Spread operator will convert [3,4] to 3,4
console.log(arr)

Copy this code and run, you should get an insight about the crux of the logic. Below explains it in detail.

Algorithm

  1. Taking the last element of the nested array,
  2. In case if it is also an array then we use spread operator to eliminate the array and push it back to array.
  3. If it is not an array then don’t use spread operator just push the value into array.

Complete code

function flatten(input) {
  const stack = [...input];
  const res = [];
  while (stack.length) {
    // pop value from stack
    const next = stack.pop();
    if (Array.isArray(next)) {
      // push back array items, won't modify the original input
      stack.push(...next);
    } else {
      res.push(next);
    }
  }
  // reverse to restore input order
  return res.reverse();
}

const arr = [1, 2, [3, 4, [5, 6]]];
flatten(arr);

Explanation:

const stack = [...input]; 
// it is just a deep copy of variable. Otherwise source array will // be modified

res variable is used to hold the final result.

return value is reversed res, because we are performing our activity from the last. So, while finally sending the result it should be reversed to make it in the actual order.

return res.reverse();

If you have not followed me on Medium, please follow. Do not forget to subscribe to my Youtube Channel,

In case if you want to talk to me personally for mock interview, tips and tricks to clear interview or resume review, you can book a session here:

If your preparing for frontEnd developer interview, please watch below series of mine:

https://www.youtube.com/watch?v=qcixpy3HQ9s&list=PLmcRO0ZwQv4QMslGJQg7N8AzaHkC5pJ4t

If you want to learn JavaScript custom implementations of built in method, then watch below series of mine:

https://www.youtube.com/watch?v=eGzErMUfdpk&list=PLmcRO0ZwQv4RWqjZCajaKBAdCJjmkAL_I

JavaScript
Interview
Recommended from ReadMedium