<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mohammed Shaikh</title>
    <description>The latest articles on DEV Community by Mohammed Shaikh (@mqshaikh8).</description>
    <link>https://dev.to/mqshaikh8</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F294314%2F7da686ed-b078-490b-bb7b-2dede21b7482.png</url>
      <title>DEV Community: Mohammed Shaikh</title>
      <link>https://dev.to/mqshaikh8</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mqshaikh8"/>
    <language>en</language>
    <item>
      <title>Tree Traversals Explained: DFS, BFS, and When to Use Each</title>
      <dc:creator>Mohammed Shaikh</dc:creator>
      <pubDate>Sun, 04 Jan 2026 14:09:25 +0000</pubDate>
      <link>https://dev.to/mqshaikh8/tree-traversals-explained-dfs-bfs-and-when-to-use-each-2m3f</link>
      <guid>https://dev.to/mqshaikh8/tree-traversals-explained-dfs-bfs-and-when-to-use-each-2m3f</guid>
      <description>&lt;h2&gt;
  
  
  Data Structures 1.2 — Tree Traversals (DFS &amp;amp; BFS)
&lt;/h2&gt;

&lt;p&gt;In &lt;strong&gt;Part 1.1&lt;/strong&gt;, we introduced trees and built our first binary tree. Now it’s time to answer an important question:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How do we actually move through a tree?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Tree traversals define the order in which nodes are visited. Mastering them is essential for understanding algorithms, solving interview questions, and working with real-world systems like the DOM, file systems, and search problems.&lt;/p&gt;

&lt;p&gt;This post focuses on the two big families of traversals:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Depth-First Search (DFS):&lt;/strong&gt; Think of &lt;strong&gt;DFS&lt;/strong&gt; like walking down one hallway until it ends, then backtracking.&lt;br&gt;
&lt;strong&gt;Breadth-First Search (BFS):&lt;/strong&gt; Think of &lt;strong&gt;BFS&lt;/strong&gt; like checking every room on the current floor before going upstairs.&lt;/p&gt;


&lt;h2&gt;
  
  
  Depth-First Search (DFS)
&lt;/h2&gt;

&lt;p&gt;Depth-First Search explores a tree by going as deep as possible before backtracking.&lt;/p&gt;

&lt;p&gt;DFS has three common variants:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Inorder&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Preorder&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Postorder&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All three follow the same recursive structure — the difference is &lt;em&gt;when&lt;/em&gt; the current node is processed.&lt;/p&gt;


&lt;h2&gt;
  
  
  Inorder Traversal (Left → Root → Right)
&lt;/h2&gt;

&lt;p&gt;Inorder traversal first visits the left subtree, then the current node, and finally the right subtree.&lt;/p&gt;

&lt;p&gt;This traversal is especially important for &lt;strong&gt;Binary Search Trees&lt;/strong&gt;, because it returns values in sorted order.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;inorderTraversal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;inorderTraversal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;inorderTraversal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When to use inorder:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Retrieving sorted data from a BST&lt;/li&gt;
&lt;li&gt;Validating BST structure&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Preorder Traversal (Root → Left → Right)
&lt;/h2&gt;

&lt;p&gt;Preorder traversal processes the current node &lt;em&gt;before&lt;/em&gt; its children.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;preorderTraversal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;preorderTraversal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;preorderTraversal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When to use preorder:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Serializing trees&lt;/li&gt;
&lt;li&gt;Copying tree structures&lt;/li&gt;
&lt;li&gt;Building expression trees&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Postorder Traversal (Left → Right → Root)
&lt;/h2&gt;

&lt;p&gt;Postorder traversal processes the current node &lt;em&gt;after&lt;/em&gt; both children.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;postorderTraversal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;postorderTraversal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;postorderTraversal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When to use postorder:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Deleting trees&lt;/li&gt;
&lt;li&gt;Evaluating expressions&lt;/li&gt;
&lt;li&gt;Bottom-up computations&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Breadth-First Search (BFS)
&lt;/h2&gt;

&lt;p&gt;Breadth-First Search traverses the tree &lt;strong&gt;level by level&lt;/strong&gt;, from left to right.&lt;/p&gt;

&lt;p&gt;Unlike DFS, BFS uses a &lt;strong&gt;queue&lt;/strong&gt; instead of recursion.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;breadthFirstTraversal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When to use BFS:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Finding the shortest path in unweighted trees&lt;/li&gt;
&lt;li&gt;Level-order processing&lt;/li&gt;
&lt;li&gt;Problems involving "nearest" or "minimum distance"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both DFS and BFS visit every node once — &lt;strong&gt;O(n)&lt;/strong&gt; time.&lt;/p&gt;

&lt;p&gt;The key difference is space:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;DFS&lt;/strong&gt; uses space proportional to the tree height (call stack)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;BFS&lt;/strong&gt; uses space proportional to the widest level (queue)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  DFS vs BFS — How to Choose?
&lt;/h2&gt;

&lt;p&gt;Here’s a simple mental model:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Use &lt;strong&gt;DFS&lt;/strong&gt; when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You want to explore deep paths&lt;/li&gt;
&lt;li&gt;You’re working recursively&lt;/li&gt;
&lt;li&gt;The tree depth is manageable&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Use &lt;strong&gt;BFS&lt;/strong&gt; when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You need the shortest path&lt;/li&gt;
&lt;li&gt;You care about levels&lt;/li&gt;
&lt;li&gt;You want the closest node&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;In practice, the choice often comes down to memory — deep but narrow trees favor DFS, while shallow but wide trees favor BFS.&lt;/p&gt;

&lt;p&gt;In interviews, keywords like &lt;em&gt;"closest"&lt;/em&gt;, &lt;em&gt;"minimum depth"&lt;/em&gt;, or &lt;em&gt;"level order"&lt;/em&gt; often hint at BFS.&lt;/p&gt;




&lt;h2&gt;
  
  
  Common Mistakes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Forgetting base cases in recursion&lt;/li&gt;
&lt;li&gt;Mixing traversal orders&lt;/li&gt;
&lt;li&gt;Using DFS where BFS is required (and vice versa)&lt;/li&gt;
&lt;li&gt;Modifying the tree unintentionally during traversal&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Interview Tip
&lt;/h2&gt;

&lt;p&gt;If you can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Write all three DFS traversals from memory&lt;/li&gt;
&lt;li&gt;Explain when to use BFS&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You already have an advantage in tree-based interview questions.&lt;/p&gt;




&lt;h2&gt;
  
  
  Continue the Data Structures Series
&lt;/h2&gt;

&lt;p&gt;This post is part of an ongoing &lt;strong&gt;Data Structures Series&lt;/strong&gt; focused on clarity, real-world intuition, and JavaScript implementations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prev:&lt;/strong&gt; &lt;a href="https://dev.to/mqshaikh8/trees-fundamentals-structure-terminology-and-use-cases-2nl4"&gt;&lt;strong&gt;Part 1.1 — Tree Fundamentals&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;View the full roadmap:&lt;/strong&gt; &lt;a href="https://dev.to/mqshaikh8/data-structures-series-a-structured-roadmap-with-javascript-407f"&gt;&lt;strong&gt;Data Structures Series — Overview&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>computerscience</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Data Structures Series: A Structured Roadmap with JavaScript</title>
      <dc:creator>Mohammed Shaikh</dc:creator>
      <pubDate>Sat, 20 Dec 2025 02:35:25 +0000</pubDate>
      <link>https://dev.to/mqshaikh8/data-structures-series-a-structured-roadmap-with-javascript-407f</link>
      <guid>https://dev.to/mqshaikh8/data-structures-series-a-structured-roadmap-with-javascript-407f</guid>
      <description>&lt;p&gt;Welcome to the &lt;strong&gt;Data Structures Series&lt;/strong&gt;, a structured, JavaScript-powered breakdown of the core data structures every developer should know.&lt;/p&gt;

&lt;p&gt;Whether you're preparing for interviews, brushing up fundamentals, or leveling up as a software engineer, this series is designed to be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Clear&lt;/strong&gt; — no fluff, just clean explanations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Practical&lt;/strong&gt; — real-world relevance, not just theory.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Code-driven&lt;/strong&gt; — every concept backed by JavaScript examples.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Structured&lt;/strong&gt; — each part builds on the previous one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Consistent&lt;/strong&gt; — two posts per week (Monday &amp;amp; Thursday).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This overview acts as your &lt;strong&gt;hub&lt;/strong&gt;, your table of contents, and your roadmap. Bookmark it — every future post will link back here.&lt;/p&gt;




&lt;h2&gt;
  
  
  What You Can Expect from This Series
&lt;/h2&gt;

&lt;p&gt;This series is broken into &lt;strong&gt;six major parts&lt;/strong&gt;, each covering a core data structure family.&lt;/p&gt;

&lt;p&gt;You’ll learn:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How each structure works&lt;/li&gt;
&lt;li&gt;Where it’s used in real software&lt;/li&gt;
&lt;li&gt;How to implement it in JavaScript&lt;/li&gt;
&lt;li&gt;Common patterns and mistakes&lt;/li&gt;
&lt;li&gt;Interview-style applications&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Published incrementally as each part is completed.&lt;/p&gt;




&lt;h2&gt;
  
  
  PART 1 — TREES
&lt;/h2&gt;

&lt;p&gt;Trees are foundational to many systems — the DOM, file systems, compilers, databases, and more.&lt;/p&gt;

&lt;p&gt;1.1 &lt;a href="https://dev.to/mqshaikh8/trees-fundamentals-structure-terminology-and-use-cases-2nl4"&gt;Trees Fundamentals&lt;/a&gt;&lt;br&gt;
1.2 &lt;a href="https://dev.to/mqshaikh8/tree-traversals-explained-dfs-bfs-and-when-to-use-each-2m3f"&gt;Tree Traversals (DFS/BFS)&lt;/a&gt;&lt;br&gt;
1.3 Binary Search Trees (BST Intro)&lt;br&gt;
1.4 BST Operations — Insert, Search, Delete&lt;br&gt;
1.5 AVL Trees (Balancing Logic)&lt;br&gt;
1.6 Red-Black Trees (Color &amp;amp; Rotation Rules)&lt;br&gt;
1.7 N-Ary Trees&lt;br&gt;
1.8 Tries (Prefix Trees)&lt;br&gt;
1.9 Heap / Priority Queue (as Trees)&lt;br&gt;
1.10 Interview-style Tree Patterns &amp;amp; Templates&lt;/p&gt;




&lt;h2&gt;
  
  
  PART 2 — HASHING &amp;amp; MAPS
&lt;/h2&gt;

&lt;p&gt;One of the most important foundations in all of programming.&lt;/p&gt;

&lt;p&gt;2.1 Hash Tables — Intro&lt;br&gt;
2.2 Hash Functions — Good vs Bad&lt;br&gt;
2.3 Collision Strategies&lt;br&gt;
2.4 Implementing a HashMap in JS&lt;br&gt;
2.5 Map vs Object vs Dictionary&lt;br&gt;
2.6 Interview-style Hashing Patterns&lt;/p&gt;




&lt;h2&gt;
  
  
  PART 3 — LINKED LISTS
&lt;/h2&gt;

&lt;p&gt;A staple in interviews, pointer problems, and memory-based structures.&lt;/p&gt;

&lt;p&gt;3.1 Singly Linked List — Intro&lt;br&gt;
3.2 Doubly &amp;amp; Circular Linked Lists&lt;br&gt;
3.3 Fast/Slow Pointer Technique&lt;br&gt;
3.4 Linked List Implementation in JS&lt;br&gt;
3.5 Interview-style LL Patterns&lt;/p&gt;




&lt;h2&gt;
  
  
  PART 4 — STACKS &amp;amp; QUEUES
&lt;/h2&gt;

&lt;p&gt;Core structures behind algorithms, parsing, UI rendering, and more.&lt;/p&gt;

&lt;p&gt;4.1 Stacks — LIFO Explained&lt;br&gt;
4.2 Queues — FIFO, Priority Queue&lt;br&gt;
4.3 Implementing Stacks &amp;amp; Queues in JS&lt;br&gt;
4.4 Monotonic Stack (Interview Applications)&lt;br&gt;
4.5 Interview-style Patterns&lt;/p&gt;




&lt;h2&gt;
  
  
  PART 5 — GRAPHS
&lt;/h2&gt;

&lt;p&gt;One of the most powerful and flexible data structures in computer science.&lt;/p&gt;

&lt;p&gt;5.1 Graph Basics&lt;br&gt;
5.2 Representations (Adjacency List / Matrix)&lt;br&gt;
5.3 BFS Deep Dive&lt;br&gt;
5.4 DFS Deep Dive&lt;br&gt;
5.5 Directed vs Undirected Graphs&lt;br&gt;
5.6 Weighted Graphs&lt;br&gt;
5.7 Dijkstra&lt;br&gt;
5.8 A* Pathfinding&lt;br&gt;
5.9 Topological Sort&lt;br&gt;
5.10 Interview-style Graph Patterns&lt;/p&gt;




&lt;h2&gt;
  
  
  PART 6 — ARRAYS &amp;amp; STRINGS
&lt;/h2&gt;

&lt;p&gt;The category where most interview questions live.&lt;/p&gt;

&lt;p&gt;6.1 Sliding Window&lt;br&gt;
6.2 Two Pointers&lt;br&gt;
6.3 Prefix Sums&lt;br&gt;
6.4 Difference Arrays&lt;br&gt;
6.5 Searching Techniques&lt;br&gt;
6.6 Sorting Algorithms Overview&lt;br&gt;
6.7 String Algorithms&lt;br&gt;
6.8 Subarray Patterns&lt;br&gt;
6.9 Kadane’s Algorithm&lt;br&gt;
6.10 Interview-style Best Array/String Problems&lt;/p&gt;




&lt;h2&gt;
  
  
  How to Follow the Series
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Posts drop Monday and Thursday.&lt;/li&gt;
&lt;li&gt;All code examples use &lt;strong&gt;JavaScript&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Each part can be read independently.&lt;/li&gt;
&lt;li&gt;Following in order builds the strongest understanding.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Bookmark this overview&lt;/li&gt;
&lt;li&gt;Comment questions you want answered&lt;/li&gt;
&lt;li&gt;Suggest topics for deeper dives&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Why This Series Exists
&lt;/h2&gt;

&lt;p&gt;I’ve learned these concepts through:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Courses&lt;/li&gt;
&lt;li&gt;Senior engineers&lt;/li&gt;
&lt;li&gt;Papers &amp;amp; articles&lt;/li&gt;
&lt;li&gt;Real-world debugging&lt;/li&gt;
&lt;li&gt;Interview preparation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This series is my attempt to make the path clearer for the next person.&lt;/p&gt;

&lt;p&gt;Clean explanations. Practical code. Developer-focused learning.&lt;/p&gt;




&lt;h2&gt;
  
  
  Stay Tuned — First Posts Dropping Soon
&lt;/h2&gt;

&lt;p&gt;Follow me to get each part as it releases.&lt;/p&gt;

&lt;p&gt;Next post: &lt;a href="https://dev.to/mqshaikh8/trees-fundamentals-structure-terminology-and-use-cases-2nl4"&gt;&lt;strong&gt;Part 1.1 — Tree Fundamentals&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's master data structures together.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Trees Fundamentals: Structure, Terminology, and Use Cases</title>
      <dc:creator>Mohammed Shaikh</dc:creator>
      <pubDate>Sat, 20 Dec 2025 02:32:17 +0000</pubDate>
      <link>https://dev.to/mqshaikh8/trees-fundamentals-structure-terminology-and-use-cases-2nl4</link>
      <guid>https://dev.to/mqshaikh8/trees-fundamentals-structure-terminology-and-use-cases-2nl4</guid>
      <description>&lt;p&gt;This post introduces &lt;strong&gt;Trees&lt;/strong&gt;, one of the most common and important data structures in computer science. Trees show up everywhere — in file systems, the DOM, compilers, databases, operating systems, pathfinding, and tons of interview questions.&lt;/p&gt;

&lt;p&gt;This is &lt;strong&gt;Part 1.1&lt;/strong&gt; of the Data Structures Series.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Is a Tree?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Tree&lt;/strong&gt; is a hierarchical, non-linear data structure made of &lt;strong&gt;nodes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Each node:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stores a value&lt;/li&gt;
&lt;li&gt;Has zero or more children&lt;/li&gt;
&lt;li&gt;Has exactly one parent (except the root)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Common real-world analogies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Folder structures&lt;/li&gt;
&lt;li&gt;Company org charts&lt;/li&gt;
&lt;li&gt;Website DOM&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Trees are ideal when the data naturally forms a hierarchy.&lt;/p&gt;




&lt;h2&gt;
  
  
  Common Types of Trees
&lt;/h2&gt;

&lt;p&gt;There are many variations of trees, but these are the ones most used in software and interviews:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Binary Tree&lt;/li&gt;
&lt;li&gt;Binary Search Tree (BST)&lt;/li&gt;
&lt;li&gt;AVL Tree&lt;/li&gt;
&lt;li&gt;Red-Black Tree&lt;/li&gt;
&lt;li&gt;N-ary Tree&lt;/li&gt;
&lt;li&gt;Trie (Prefix Tree)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This post focuses on the &lt;strong&gt;Binary Tree&lt;/strong&gt;, since most traversal logic starts here.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Is a Binary Tree?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Binary Tree&lt;/strong&gt; is a tree where each node has at most &lt;strong&gt;two children&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's create one in JavaScript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// The tree structure:&lt;/span&gt;
&lt;span class="c1"&gt;//        1&lt;/span&gt;
&lt;span class="c1"&gt;//       / \&lt;/span&gt;
&lt;span class="c1"&gt;//      2   3&lt;/span&gt;
&lt;span class="c1"&gt;//     / \&lt;/span&gt;
&lt;span class="c1"&gt;//    4   5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also build the tree with nested constructor calls, but this form is more readable for beginners.&lt;/p&gt;




&lt;h2&gt;
  
  
  Tree Traversals
&lt;/h2&gt;

&lt;p&gt;Traversals describe how we "walk" through a tree. There are four fundamental traversal patterns:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Inorder&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Preorder&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Postorder&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Breadth-First Search (Level Order)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We'll go deeper into each in &lt;strong&gt;Part 1.2&lt;/strong&gt;, but here is a quick preview.&lt;/p&gt;




&lt;h2&gt;
  
  
  Inorder (Left → Root → Right)
&lt;/h2&gt;

&lt;p&gt;Useful in Binary Search Trees because it returns nodes in sorted order.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;inorderTraversal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;inorderTraversal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;inorderTraversal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Preorder (Root → Left → Right)
&lt;/h2&gt;

&lt;p&gt;Good for serializing or copying trees.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;preorderTraversal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;preorderTraversal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;preorderTraversal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Postorder (Left → Right → Root)
&lt;/h2&gt;

&lt;p&gt;Commonly used when deleting or evaluating trees.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;postorderTraversal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;postorderTraversal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;postorderTraversal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Breadth-First Search (Level Order)
&lt;/h2&gt;

&lt;p&gt;Visits each level of the tree from left to right.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;breadthFirstTraversal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;currentNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Why Traversals Matter
&lt;/h2&gt;

&lt;p&gt;Different problems require different traversal strategies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Inorder&lt;/strong&gt; → sorted output in BSTs&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Preorder&lt;/strong&gt; → reconstructing or serializing trees&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Postorder&lt;/strong&gt; → evaluating expressions, deleting structures&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;BFS&lt;/strong&gt; → shortest path, level-by-level processing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You'll see these patterns repeatedly in interviews.&lt;/p&gt;







&lt;h2&gt;
  
  
  Continue the Data Structures Series
&lt;/h2&gt;

&lt;p&gt;This post is part of an ongoing &lt;strong&gt;Data Structures Series&lt;/strong&gt; focused on clarity, real-world intuition, and JavaScript implementations.&lt;/p&gt;

&lt;p&gt;View the full roadmap:  &lt;/p&gt;

&lt;h2&gt;
  
  
  👉 &lt;a href="https://dev.to/mqshaikh8/data-structures-series-a-structured-roadmap-with-javascript-407f"&gt;Data Structures Series — Overview&lt;/a&gt;
&lt;/h2&gt;

</description>
      <category>algorithms</category>
      <category>javascript</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>987. Vertical Order Traversal of a Binary Tree (HARD)</title>
      <dc:creator>Mohammed Shaikh</dc:creator>
      <pubDate>Mon, 19 Feb 2024 12:34:58 +0000</pubDate>
      <link>https://dev.to/mqshaikh8/987-vertical-order-traversal-of-a-binary-tree-hard-5fjd</link>
      <guid>https://dev.to/mqshaikh8/987-vertical-order-traversal-of-a-binary-tree-hard-5fjd</guid>
      <description>&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;This is a hard problem on Leetcode. This is an interesting twist to the usual traversal problems with an acceptance rating of 47%&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Falx46mhqji2gll13e6zl.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Falx46mhqji2gll13e6zl.gif" alt="Lets get this party started giphy" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Given the root of a binary tree, we have to calculate the vertical order traversal of the binary tree.&lt;/p&gt;

&lt;p&gt;For each node at position (row, col), its left and right children will be at positions (row + 1, col - 1) and (row + 1, col + 1) respectively. The root of the tree is at (0, 0).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;What is vertical order traversal&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The vertical order traversal of a binary tree is a list of top-to-bottom orderings for each column index starting from the leftmost column and ending on the rightmost column. There may be multiple nodes in the same row and same column. In such a case, sort these nodes by their values.&lt;/p&gt;

&lt;p&gt;So given a binary tree that looks like&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb9fzvazqnna977ij50et.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb9fzvazqnna977ij50et.png" alt="sample binary ree" width="800" height="566"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The left most node is 9 and the right most node is 7. The top most node(aka root) is 3 and there are two nodes that would be considered bottom most, 15 and 7.&lt;/p&gt;

&lt;p&gt;Since in vertical order traversal, we do top to bottom and left to right. The return value will look like &lt;code&gt;[[9],[3,15],[20],[7]]&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;A good way to solve this would be to do a DFS(depth first search). I chose to do this recursively. We can also do this iteratively, which we will cover later.&lt;/p&gt;

&lt;p&gt;In order to solve this, we have to divide the problem in two parts.&lt;br&gt;
&lt;/p&gt;
&lt;ul&gt;

&lt;li&gt; Figuring out which node goes into what column and rows&lt;/li&gt;

&lt;li&gt; Putting all the nodes together in the vertical traversal order&lt;/li&gt;

&lt;ul&gt;

&lt;p&gt;The first part can be done by going through the binary tree and the second part is putting it together in the right order.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;first part&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;/ul&gt;
&lt;/ul&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;verticalTraversal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;columnMap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dfs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;row&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;col&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;columnMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;col&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;columnMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;col&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;columnMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;col&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;val&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;row&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

        &lt;span class="nf"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;row&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;col&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nf"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;row&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;col&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="nf"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;This is a standard recursive function and it goes through and captures all the nodes by column and also stores its row. You can use a plain object for this as well. The column map will look something like&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Now we know all the nodes and their respective columns and rows. We have to just turn that information into an ordered array of arrays.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;columnMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;()].&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;nodes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;nodes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;row&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;row&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;row&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;row&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nodes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;There is a lot of things happening here so let's break it down.&lt;/p&gt;

&lt;p&gt;First we have the sort function &lt;code&gt;[...columnMap.entries()].sort((a, b) =&amp;gt; a[0] - b[0])&lt;/code&gt;. It takes all the keyValue pairs, puts them in an array and sorts them by their columns(keys).&lt;/p&gt;

&lt;p&gt;Then we have the massive map function. There are three parts to this. &lt;/p&gt;

&lt;p&gt;1) The main map: We have to mmap through the large key + array of objects and turn it into a simple array of arrays. We are working through each value pair and running the the sort and the map on the values.&lt;/p&gt;

&lt;p&gt;2) The sort function makes sure that the nodes are in the vertical traversal order. Sort the nodes by rows and values&lt;/p&gt;

&lt;p&gt;3) the map gets rid of all values about the node except the nodeValue. puts it in an array and returns it to the bigger arr.&lt;/p&gt;

&lt;p&gt;at the end of this, you will have all the information need for this problem&lt;/p&gt;

&lt;p&gt;There are other solutions like doing BFS iteratively and many other variations. This was my favorite because I feel like it is as efficient as most of them, but it is very simple and straight forward&lt;/p&gt;

&lt;p&gt;Thanks for reading. Hope it helps!&lt;/p&gt;



</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Iterators in JAVA</title>
      <dc:creator>Mohammed Shaikh</dc:creator>
      <pubDate>Wed, 14 Feb 2024 16:25:00 +0000</pubDate>
      <link>https://dev.to/mqshaikh8/iterators-in-java-2gj0</link>
      <guid>https://dev.to/mqshaikh8/iterators-in-java-2gj0</guid>
      <description>&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;When I was going through and learning Java. I wished there was a collection of docs that was written that explains Java topics simply.&lt;/p&gt;

&lt;h3&gt;
  
  
  Iterating through arrays
&lt;/h3&gt;


&lt;li&gt;For Loop:&lt;/li&gt;
&lt;br&gt;
This is the standard for-loop. This is in a lot of different languages as well&lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[] myArray = {1, 2, 3, 4, 5};
for (int i = 0; i &amp;lt; myArray.length; i++) {
    System.out.println(myArray[i]); // logic to log element
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;li&gt;Enhanced For Loop (for-each):&lt;/li&gt;
&lt;br&gt;
A more concise way to iterate through an array, especially beneficial when you don't need the index.&lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[] myArray = {1, 2, 3, 4, 5};
for (int element : myArray) {
    System.out.println(element);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;li&gt;Arrays.stream: &lt;/li&gt;
&lt;br&gt;
The Streams API introduced in Java 8 offers a functional approach to array iteration.&lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[] myArray = {1, 2, 3, 4, 5};
Arrays.stream(myArray).forEach(System.out::println);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;li&gt;ListIterator: &lt;/li&gt;
&lt;br&gt;
Ideal for iterating backward through a List, providing more control over traversal.&lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;List&amp;lt;Integer&amp;gt; myList = Arrays.asList(1, 2, 3, 4, 5);
ListIterator&amp;lt;Integer&amp;gt; iterator = myList.listIterator(myList.size());
while (iterator.hasPrevious()) {
    System.out.println(iterator.previous());
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;h3&gt;
  
  
  Iterating through objects
&lt;/h3&gt;


&lt;li&gt;Using Map.Entry:&lt;/li&gt;
&lt;br&gt;
For iterating through key-value pairs in a Map, Map.Entry allows access to both the key and the value.&lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Map&amp;lt;String, Integer&amp;gt; myMap = new HashMap&amp;lt;&amp;gt;();
myMap.put("a", 1);
myMap.put("b", 2);
myMap.put("c", 3);

for (Map.Entry&amp;lt;String, Integer&amp;gt; entry : myMap.entrySet()) {
    System.out.println(entry.getKey() + ": " + entry.getValue());
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;li&gt;Using Java Streams API:&lt;/li&gt;
&lt;br&gt;
Leveraging the powerful Streams API for concise and functional iteration through Map entries.&lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Map&amp;lt;String, Integer&amp;gt; myMap = new HashMap&amp;lt;&amp;gt;();
myMap.put("a", 1);
myMap.put("b", 2);
myMap.put("c", 3);

myMap.entrySet().stream().forEach(entry -&amp;gt; {
    System.out.println(entry.getKey() + ": " + entry.getValue());
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fze8eoevm7tlc6ywbs6rq.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fze8eoevm7tlc6ywbs6rq.gif" alt="let us begin now giphy" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Things to look out for
&lt;/h3&gt;


&lt;li&gt; Immutable vs Mutable List &lt;/li&gt;
&lt;br&gt;
Understanding the distinction between immutable and mutable lists is crucial. An immutable list cannot be modified after creation, while a mutable list allows changes. Consider your use case and requirements when choosing between them. 

&lt;p&gt;&lt;br&gt;
Thank you for reading! I hope this information proves helpful.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>1679. Max Number of K-Sum Pairs</title>
      <dc:creator>Mohammed Shaikh</dc:creator>
      <pubDate>Mon, 12 Feb 2024 17:52:27 +0000</pubDate>
      <link>https://dev.to/mqshaikh8/1679-max-number-of-k-sum-pairs-4718</link>
      <guid>https://dev.to/mqshaikh8/1679-max-number-of-k-sum-pairs-4718</guid>
      <description>&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;In this article, we will explore a problem and walk through various solutions. If you are looking for the solutions directly, feel free to scroll down to the respective sections.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fddz7rw8jg3ygyd4nwj3s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fddz7rw8jg3ygyd4nwj3s.png" alt="problem_statement" width="800" height="523"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;First thing we do is summarise the problem statement and remove all the fluff.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Find out how many combinations add up to `k`&lt;/li&gt;
&lt;li&gt;The combinations have to be unique&lt;/li&gt;
&lt;li&gt;The array is always going to be more than 1 in length and made up of positive integers&lt;/li&gt;
&lt;li&gt;`k` will always be more than 1&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;With this summary, we can get started crafting our brute force solution for this problem. A lot of people start trying to find the most optimized solution. In the beginning(of your ds&amp;amp;A journey), it is better to first craft brute force and then optimize&lt;/p&gt;

&lt;h2&gt;
  
  
  Brute Force Solution
&lt;/h2&gt;

&lt;p&gt;An easy way I have found that helps people come up with a solution is imagining how you would do it in real life. If you see the input and you were asked the question.&lt;/p&gt;

&lt;p&gt;If someone had to do this in real life, these are the things we do:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;pick element A (starting at the first) and check against all elements.&lt;/li&gt;
&lt;li&gt;If there is a match(equals to K), we mentally add 1 to the count of operations &lt;/li&gt;
&lt;li&gt; We can then set element A to the next variable&lt;/li&gt;
&lt;li&gt;We will have to delete the elements at those index&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now we can start coding!!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F551ouw8ai0ndl46grenh.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F551ouw8ai0ndl46grenh.gif" alt="let us begin now giphy" width="500" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First we have to build the logic for adding and checking each elements of the arr with others&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const maxOperations =(nums, k) =&amp;gt; {
    for(let i = 0; i &amp;lt; nums.length; i++){
        for(let j = i; j &amp;lt; nums.length; j++){
            if(nums[i] + nums[j] === k){
                console.log(nums[i], i, 'maxOperations', nums[j], j)
            }
        }
    }
}
maxOperations([3,1,3,4,3], 6)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are comparing all elements with other elements and check if it equals &lt;code&gt;k&lt;/code&gt;. When you run this code, you will notice it console.log 6 times even though the expected answer is 1.&lt;/p&gt;

&lt;p&gt;That is because we did not handle the removal of elements from the array, so we basically are double/triple counting. Lets fix that.&lt;/p&gt;

&lt;p&gt;What we have to do is remove the two elements from the logic. The simplest way to do that is making both the elements equal to something else. I will make those elements as &lt;code&gt;NaN&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   nums[i] = NaN;
   nums[j] = NaN;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets put it all together with the counter logic.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const maxOperations = (nums, k) =&amp;gt; {
    let counter = 0
    for (let i = 0; i &amp;lt; nums.length; i++) {
        for (let j = i + 1; j &amp;lt; nums.length; j++) {
            if (nums[i] + nums[j] === k) {
                counter += 1

                nums[i] = NaN;
                nums[j] = NaN;
            }
        }
    }
    return counter
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The time complexity is &lt;code&gt;O(N^2)&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Alternative Solutions
&lt;/h2&gt;

&lt;p&gt;There are different solutions that we can do that are more optimized.&lt;/p&gt;

&lt;h3&gt;
  
  
  Approach 1: Hash Table - Time: O(N)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function maxOperationsHashMap(nums, k) {
    const frequencyMap = new Map();
    let operationsCount = 0;

    for (const num of nums) {
        const complement = k - num;
        if (frequencyMap.has(complement) &amp;amp;&amp;amp; frequencyMap.get(complement) &amp;gt; 0) {
            operationsCount++;
            frequencyMap.set(complement, frequencyMap.get(complement) - 1);
        } else {
            frequencyMap.set(num, (frequencyMap.get(num) || 0) + 1);
        }
    }

    return operationsCount;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Approach 2: Two Pointers - Time: O(N)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function maxOperationsTwoPointers(nums, k) {
    nums.sort((a, b) =&amp;gt; a - b);

    let left = 0;
    let right = nums.length - 1;
    let operationsCount = 0;

    while (left &amp;lt; right) {
        const sum = nums[left] + nums[right];

        if (sum === k) {
            operationsCount++;
            left++;
            right--;
        } else if (sum &amp;lt; k) {
            left++;
        } else {
            right--;
        }
    }

    return operationsCount;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Approach 3: Count Frequencies with a Hash Table - Time: O(N)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function maxOperationsCountFrequencies(nums, k) {
    const frequencyMap = new Map();
    let operationsCount = 0;

    for (const num of nums) {
        frequencyMap.set(num, (frequencyMap.get(num) || 0) + 1);
    }

    for (const num of nums) {
        const complement = k - num;

        if (frequencyMap.has(complement) &amp;amp;&amp;amp; frequencyMap.get(complement) &amp;gt; 0 &amp;amp;&amp;amp; frequencyMap.get(num) &amp;gt; 0) {
            operationsCount++;
            frequencyMap.set(num, frequencyMap.get(num) - 1);
            frequencyMap.set(complement, frequencyMap.get(complement) - 1);
        }
    }
    return operationsCount;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;All of the three solutions we talked about here are O(N). I just wanted to show the different ways to solve. It helps to know that in case there are variations to the problem. &lt;/p&gt;

&lt;p&gt;Thank you for reading! I hope this information proves helpful.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Algorithms: Common Years Problem</title>
      <dc:creator>Mohammed Shaikh</dc:creator>
      <pubDate>Tue, 23 Jun 2020 22:09:54 +0000</pubDate>
      <link>https://dev.to/mqshaikh8/algorithms-common-years-problem-6ld</link>
      <guid>https://dev.to/mqshaikh8/algorithms-common-years-problem-6ld</guid>
      <description>&lt;p&gt;I wanted to write about an interesting question I faced in a technical interview:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Given an array of arrays containing birth years and death year of people find out the most common years amongst them.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; [[1901,1950],[1930,1960],[1973,2022]]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The steps I use to solve any algorithm problem:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Find out how I would do it manually.&lt;/li&gt;
&lt;li&gt;Write them down as steps.&lt;/li&gt;
&lt;li&gt;Translate into code.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  1. Find out how I would do it manually.
&lt;/h2&gt;

&lt;p&gt;The manual way would be to go through each set and remember the range.for ex, the first set is from 1901 to 1950. At the back of our mind, we can remember those years as occurring once, then we go on to the next set. We remember the next set so we can already see the overlap. The first two have been alive together from 1930 to 1950. Then, we move on to the third and last set and we can see that it does not overlap with the other two.&lt;/p&gt;

&lt;p&gt;Few things to note. Firstly, we need to connect occurrences to years. Secondly, the years with the most occurrences are the maximum occurences.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Write them down as steps.
&lt;/h2&gt;

&lt;p&gt;To solve this problem, we need to connect each year with the amount of occurences. Afterwards, we need to find the max occurence and find out all the years that have that.&lt;/p&gt;

&lt;p&gt;I decided to use hash to connect&lt;code&gt;{1901 =&amp;gt; 1}&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Translate into code.
&lt;/h2&gt;

&lt;p&gt;This is code version of the steps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def findCommonYear(arr)
    years = {}
    arr.each do |year_set|
        start_year = year_set[0]
        end_year = year_set[1]
        while start_year &amp;lt;= end_year do
            puts start_year
            if years[start_year]
                years[start_year] += 1
            else
                years[start_year] = 1
            end
            start_year += 1
        end
    end
    if years.values.max != 1 
        max_common_count = years.values.max
    else
        return 0
    end
    max_common_years = []
    years.each do |key,value|
        if value == max_common_count
            max_common_years &amp;lt;&amp;lt; key
        end
    end
    return max_common_years
end

puts findCommonYear([[1910, 1950], [1900, 1951], [1945, 2000]])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>ruby</category>
      <category>interview</category>
      <category>algorithms</category>
      <category>walkthrough</category>
    </item>
    <item>
      <title>Animation with vanilla css</title>
      <dc:creator>Mohammed Shaikh</dc:creator>
      <pubDate>Sun, 15 Mar 2020 02:55:21 +0000</pubDate>
      <link>https://dev.to/mqshaikh8/animation-with-vanilla-css-52p1</link>
      <guid>https://dev.to/mqshaikh8/animation-with-vanilla-css-52p1</guid>
      <description>&lt;p&gt;&lt;a href="https://i.giphy.com/media/YnNgiVoHgtjDrVPbES/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/YnNgiVoHgtjDrVPbES/giphy.gif" alt="A map shaking"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Animation makes our app look a little exciting and it is very easy to do it with plain css. The first thing we need is the  &lt;code&gt;@keyframes&lt;/code&gt; css keyword. what that keyword does is that it lets us control an element on the dom. We can tell the dom to do a certain action in exact intervals. We measure intervals in percentages.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  @keyframes shake {
    0% { transform: translate(1px, 1px) rotate(0deg); }
    10% { transform: translate(-1px, -2px) rotate(-1deg); }
    20% { transform: translate(-3px, 0px) rotate(1deg); }
    30% { transform: translate(3px, 2px) rotate(0deg); }
    40% { transform: translate(1px, -1px) rotate(1deg); }
    50% { transform: translate(-1px, 2px) rotate(-1deg); }
    60% { transform: translate(-3px, 1px) rotate(0deg); }
    70% { transform: translate(3px, 1px) rotate(-1deg); }
    80% { transform: translate(-1px, -1px) rotate(1deg); }
    90% { transform: translate(1px, 2px) rotate(0deg); }
    100% { transform: translate(1px, -2px) rotate(-1deg); }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The whole animation is considered 100%.If the change we want should happen in the beginning we put it at 0%. If we want it to be halfway through the animation, we put it at 50% and at 100%, if that is the last thing we want done. We started from the beginning(0%) and told the element to move 1px on the x-axis and 1px on y-axis and rotate 0 deg and so on and so forth.&lt;/p&gt;

&lt;p&gt;The next thing we have to do is bind the function to an element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#map:hover {
    /* Start the shake animation and make the animation last for 0.5 seconds */
    animation: shake 0.5s;

    /* When the animation is finished, start again */
    animation-iteration-count: infinite;
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I put an event listener on the map, so when someone hovers over it this css blocks get triggered. &lt;code&gt;animation: shake 0.5s;&lt;/code&gt; There are three arguments on this line, first is the animation keyword, then it is the name of the function we wrote above  and the length of how long it should run.A thing to note, the percentages are of the time we mention in this command. 50% in keyframes is 50% of 0.5s in our example.&lt;/p&gt;

&lt;p&gt;There are many things possible with @keyframes. We could even change the colors programmatically&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@keyframes colorChange {
  0%   {background-color: red;}
  25%  {background-color: yellow;}
  50%  {background-color: blue;}
  100% {background-color: green;}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use negative numbers to make it look like the animation was already playing and if leave out 0% and 100% it will automatically make those two points equal to the beginning state of the element and it will start and end the animation at the same state&lt;/p&gt;

&lt;p&gt;Yay!! Now you know how to add animation using css. Hopefully it will bring as much joy to your projects as it did to mine&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Scopes in Javascript</title>
      <dc:creator>Mohammed Shaikh</dc:creator>
      <pubDate>Thu, 16 Jan 2020 13:51:20 +0000</pubDate>
      <link>https://dev.to/mqshaikh8/scopes-in-javascript-1hd7</link>
      <guid>https://dev.to/mqshaikh8/scopes-in-javascript-1hd7</guid>
      <description>&lt;p&gt;There are 4 types of scopes in JavaScript. The global, function, block, and lexical scopes.&lt;/p&gt;

&lt;p&gt;Variables in the global scopes can be accessed anywhere in the code. They are declared by not adding the variable declaration term (var, let, const) before the identifier.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function funcWithGlobalVariable(){
  globalVariable = “I am declared without the identifier”
}

function test2(){
  console.log(`${globalVariable} is the best`)
}
funcWithGlobalVariable()
test2()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are declaring the variable in function test and access in test2. However, if we declare it as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function funcWithGlobalVariable(){
  globalVariable = “I am declared without the identifier”
}

function test2(){
  console.log(`${globalVariable} is the best`)
}
funcWithGlobalVariable()
test2()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This would error out because the declaration and the invocation of the variable are in different scopes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ReferenceError: bestMod is not defined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Variables in function scope exist inside the function. All variables that are declared inside the curly brackets, stay in the curly brackets. Continuing from the example above:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function test(){
  let bestMod = "mod3"
}

function test2(){
  console.log(`${bestMod} is the best`)
}
test()
test2()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;let,const and var make the variable function scoped. The above code will give the same error as above.&lt;/p&gt;

&lt;p&gt;Variables in block scope exists inside a block of code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for(let i=0;i&amp;lt;10;i++){
   ...
}
console.log(i)//this will give an error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The "i" does not exist outside of the for loop, so the console.log line will return an error.&lt;/p&gt;

&lt;p&gt;Last but not least, lexical scope. Lexical scope means that in the hierarchy of code, everything under the level of where it was declared can access the variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function test(){
  let bestMod = "mod3"
   test2()


    function test2(){
      console.log(`${bestMod} is the best`)
    }
}


test()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code will work because "bestMod" used in "test2" is declared in the function that is wrapping it. This scope was the hardest for me to remember coming from ruby where I would have to pass it in to the function for me to be able to access it.&lt;/p&gt;

&lt;p&gt;In all of the four scopes, it is better to go from the lowest to the highest. It is always a good practice to keep the variables in block scope or function scope. Global scope, no matter how enticing it may be, is always to be avoided.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Pseudo coding</title>
      <dc:creator>Mohammed Shaikh</dc:creator>
      <pubDate>Sun, 05 Jan 2020 18:15:44 +0000</pubDate>
      <link>https://dev.to/mqshaikh8/pseudo-coding-and-its-benefits-4fa0</link>
      <guid>https://dev.to/mqshaikh8/pseudo-coding-and-its-benefits-4fa0</guid>
      <description>&lt;p&gt;What is pseudo coding?&lt;/p&gt;

&lt;p&gt;Pseudo coding is writing down what you are about to code in plain english. It is an important part of programming, especially for those that are starting out and for programmers that are encountering hard problems. It is also a great way to deal with imposter syndrome.&lt;/p&gt;

&lt;p&gt;A lot of problems seem overwhelming as a whole. When I had to make a fake facebook(&lt;a href="https://kazam-inc.herokuapp.com/" rel="noopener noreferrer"&gt;https://kazam-inc.herokuapp.com/&lt;/a&gt;) for my mod 2 project. I was very nervous and I did not know if I could do it. When I broke everything down to small pieces, Alhamdulillah, It looked very easy.&lt;/p&gt;

&lt;p&gt;I am going to start learning javascript in my bootcamp next week, so in anticipation of that. I first pseudo coded a simple app then I coded in javascript.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Ask user some question
//save the user answer
//add the answer as a part of a sentence
//print the formatted sentence
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is important, when pseudo coding, to make each step very simple. A good test to see if the pseudo code is simple enough is that a child 8-11 yrs old can follow the steps and do the action in real life. Another thing to keep in mind is the fact that there should be no code in the pseudo code. After pseudo coding, now we can go step by step and google and write everything.&lt;/p&gt;

&lt;p&gt;The first step is to ask the user for input.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;prompt("Please enter your name", "");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above line will prompt the user for their name. Now, we can move on to the next step in our pseudo code. We have to save the input to a variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const name = prompt("Please enter your name", "");
const age = prompt("Please enter your age", "");
const language = prompt("Please enter your programming language", "");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are prompting the user for three different input and saving the input in three seperate variable. Now we can cross off the first two steps.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const formattedString = (`Hello ! my name is ${name} and I am ${age} year(s) old, the programming language I like is ${language}`);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code will format the string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(formattedString);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Last but not least the above code will print it to the user. &lt;/p&gt;

&lt;p&gt;Many times people get overwhelmed by an issue they are tackling, especially when encountering a new language. However, if the problem is broken down into small steps and written down, It can helps us understand and solve any problems that we might encounter in a very efficient manner.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Python Calculator CLI App</title>
      <dc:creator>Mohammed Shaikh</dc:creator>
      <pubDate>Sun, 29 Dec 2019 22:26:49 +0000</pubDate>
      <link>https://dev.to/mqshaikh8/python-calculator-cli-app-kda</link>
      <guid>https://dev.to/mqshaikh8/python-calculator-cli-app-kda</guid>
      <description>&lt;p&gt;Before we start our calculator, It is always good practice to breakdown the steps that we would require to build it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    -[]Welcome the user
    -[]Ask him to select an operation
    -[]Save that input
    -[]Ask him for the two numbers
    -[]Save those two numbers
    -[]make arithmetic operation functions
    -[]send the appropriate functions the number
    -[]Display the number back to the user 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First on that list would be to welcome the user. Let's print a nice welcoming message&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print('Hello! User. Welcome to The Calculator')&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;When we run our program, it will print our welcoming message. The next step is to ask the user to select an operation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print('Hello! User. Welcome to The Calculator')
print("Pick an operation(1-Add)(2-Subtract)(3-Multiply)(4 - Divide)")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That 2/8 of the deliverables done. The next step is for us to receive input and save it to a variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print('Hello! User. Welcome to The Calculator')
print("Pick an operation(1-Add)(2-Subtract)(3-Multiply)(4 - Divide)")
operation = input("Enter choice(1/2/3/4): ")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The reason we did not just ask the user to input the operation name directly rather than the number is to avoid edge cases. If we tell the user to input the operation name, the possible inputs could be  "add" or "aDD" or "Add" or "ADD". The more input, the more chance of an accidental typo that can break our program. For simple applications, it is better to restrict inputs to the bare minimum so we do not spend a large time formatting the input.&lt;br&gt;
    The next step on the list is to ask for the two numbers that we are going to work with.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print('Hello! User. Welcome to The Calculator')
print("Pick an operation(1-Add)(2-Subtract)(3-Multiply)(4 - Divide)")
operation = input("Enter choice(1/2/3/4): ")
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are asking for the numbers and saving it to two variables. One of the benefits of pseudo coding properly is that it becomes a good measurement of progress. Now we know we are more than halfway done because we have finished 5 out of the 8 steps&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    -[x]Welcome the user
    -[x]Ask him to select an operation
    -[x]Save that input
    -[x]Ask him for the two numbers
    -[x]Save those two numbers
    -[]make arithmetic operation functions
    -[]send the appropriate functions the number
    -[]Display the number back to the user 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The next step is to make the code able to do the operations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def add(x, y):
  print(f'{x} + {y} = {x+y}')
# This function subtracts two numbers 
def subtract(x, y):
  print(f'{x} - {y} = {x-y}')
# This function multiplies two numbers
def multiply(x, y):
  print(f'{x} * {y} = {x*y}')
# This function divides two numbers
def divide(x, y):
  print(f'{x} / {y} = {x+y}')
print('Hello! User. Welcome to The Calculator')
print("Pick an operation(1-Add)(2-Subtract)(3-Multiply)(4 - Divide)")
operation = input("Enter choice(1/2/3/4): ")
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We made 4 functions for each of the arithmetic operations. Whatever the user chooses we can send the numbers to that function and it will take care of it for us.The next step is to direct the numbers to the appropriate function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def add(x, y):
  print(f'{x} + {y} = {x+y}')
# This function subtracts two numbers 
def subtract(x, y):
  print(f'{x} - {y} = {x-y}')
# This function multiplies two numbers
def multiply(x, y):
  print(f'{x} * {y} = {x*y}')
# This function divides two numbers
def divide(x, y):
  print(f'{x} / {y} = {x+y}')
print('Hello! User. Welcome to The Calculator')
print("Pick an operation(1-Add)(2-Subtract)(3-Multiply)(4 - Divide)")
operation = input("Enter choice(1/2/3/4): ")
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
if choice == '1':
   add(num1,num2)
elif choice == '2':
   subtract(num1,num2)
elif choice == '3':
   multiply(num1,num2)
elif choice == '4':
   divide(num1,num2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The if statements would direct each of the 4 inputs to its appropriate function. If the user wants to add, they will input 1 and it would hit the first condition. The first condition would call and pass the number to the add function. The add function will display the added number in a formatted answer.&lt;/p&gt;

&lt;p&gt;The above code will work , but it is messy. We need it to organize it better. Readability is very important for code. It is possible to add comments to organize and make it more readable, but good function and variable names and proper spacing does a better job.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def add(x, y):
  print(f'{x} + {y} = {x+y}')

def subtract(x, y):
  print(f'{x} - {y} = {x-y}')

def multiply(x, y):
  print(f'{x} * {y} = {x*y}')

def divide(x, y):
  print(f'{x} / {y} = {x+y}')

print('Hello! User. Welcome to The Calculator')
print("Pick an operation(1-Add)(2-Subtract)(3-Multiply)(4 - Divide)")

operation = input("Enter choice(1/2/3/4): ")
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")

if choice == '1':
   add(num1,num2)
elif choice == '2':
   subtract(num1,num2)
elif choice == '3':
   multiply(num1,num2)
elif choice == '4':
   divide(num1,num2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is the end of the tutorial. Hopefully everyone that reads it enjoys it as much as i enjoyed writing it.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>MVC</title>
      <dc:creator>Mohammed Shaikh</dc:creator>
      <pubDate>Tue, 17 Dec 2019 15:59:22 +0000</pubDate>
      <link>https://dev.to/mqshaikh8/mvc-g1d</link>
      <guid>https://dev.to/mqshaikh8/mvc-g1d</guid>
      <description>&lt;p&gt;MVC&lt;br&gt;
 &lt;em&gt;What is M.V.C?&lt;/em&gt;&lt;br&gt;
MVC (Model-View-Controller) is a way to structure a web-app folder where the model deals with the backend, the view deals with the front end, and the controller deals with connecting everything. A common example that is given for MVC is the restaurant example. The chef (Model) creates food (instances of a class) and manages the inventory (database). The waiter (Controller) will take the order (request) from the customer (View) and take it to the chef (Model) and then it will take the order (request) back to the customer (View). The customer (View) can make the order (request).&lt;/p&gt;

&lt;p&gt; &lt;em&gt;What is Model?&lt;/em&gt;&lt;br&gt;
Model is the data management system of the program. Model also will contain the majority of the logic. The rails command to generate a model would be:&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;rails g model (Model name) (table column name):(column data type)&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
If we were to code the MVC example, we would do&lt;br&gt;
&lt;br&gt;
&lt;code&gt;rails g model Restaurant name:string address:string&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
.The above command would output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;invoke  active_record
create    db/migrate/20191216200109_create_restaurants.rb
create    app/models/restaurant.rb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The models/restaurant.rb file is where your restaurant class will go and the db/migrate/20191216200109_create_restaurants.rb file is where your data table goes. Combined, they make the model in MVC. For more information on generators  &lt;a href="https://lmgtfy.com/?q=Rails+generators" rel="noopener noreferrer"&gt;https://lmgtfy.com/?q=Rails+generators&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt; &lt;em&gt;What is the view?&lt;/em&gt;&lt;br&gt;
View is the frontend part of your code that will contain code that generates what your user will see. If we continue the example from above, we could put:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;create  app/controllers/restaurants_controller.rb
route  get 'restaurants/new'
get 'restaurants/create'
get 'restaurants/edit'
get 'restaurants/update'
get 'restaurants/destroy'
get 'restaurants/index'
get 'restaurants/show'
      invoke  erb
      create    app/views/restaurants
      create    app/views/restaurants/new.html.erb
      create    app/views/restaurants/create.html.erb
      create    app/views/restaurants/edit.html.erb
      create    app/views/restaurants/update.html.erb
      create    app/views/restaurants/destroy.html.erb
      create    app/views/restaurants/index.html.erb
      create    app/views/restaurants/show.html.erb
      invoke  helper
      create    app/helpers/restaurants_helper.rb
      invoke  assets
      invoke    scss
      create      app/assets/stylesheets/restaurants.scss
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The controller command creates the 7 RESTful routes for restaurant and creates a view file and a controller method for each route. It is a very useful tool. There is one drawback to it, though. It creates a view file for create, update, and destroy. As soon as the command creates those files, you may have to delete it. &lt;/p&gt;

&lt;p&gt; &lt;em&gt;What is the controller?&lt;/em&gt;&lt;br&gt;
Controller is the code that connects both side, Model and View. When the request is made by browser, it first goes to the Controller, then it is sent to Model or View depending on what is needed for that request. The above command created the Controller for us already.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class RestaurantsController &amp;lt; ApplicationController
  def new
  end

  def create
  end

  def edit
  end

  def update
  end

  def destroy
  end

  def index
  end

  def show
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the user searches a url on our website, it will come to one of these methods. For example, if the user searches the home page, it will go to the index method. The index method will take the request and do whatever logic we put in that method and then send it off to the Model if needed. if not, then it will send it off to the user the correct View file.&lt;/p&gt;

&lt;p&gt;Model, View, and Controller is a way of applying the concept of separation of concerns, where every piece of code is just responsible for one thing. If there is a need to debug or update, we would know exactly where everything is, so it would make our lives easier for us. MVC also allows teams to work together on the same app without git conflicts&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
