<script data-pm-proxy="intercept"></script><?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[Harpreet's Newsletter]]></title><description><![CDATA[Sign up to receive my 𝗚𝗼𝗼𝗴𝗹𝗲 𝗥𝗲𝘀𝘂𝗺𝗲 + 𝗧𝗲𝗺𝗽𝗹𝗮𝘁𝗲 for FREE]]></description><link>https://singhz.substack.com</link><image><url>https://substackcdn.com/image/fetch/$s_!4kg6!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1f62d7e-4727-46a2-acc6-57127549e85e_800x800.png</url><title>Harpreet&apos;s Newsletter</title><link>https://singhz.substack.com</link></image><generator>Substack</generator><lastBuildDate>Tue, 01 Sep 2026 10:16:31 GMT</lastBuildDate><atom:link href="/__u/singhz.substack.com/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Harpreet Singh]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[singhz@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[singhz@substack.com]]></itunes:email><itunes:name><![CDATA[Harpreet Singh]]></itunes:name></itunes:owner><itunes:author><![CDATA[Harpreet Singh]]></itunes:author><googleplay:owner><![CDATA[singhz@substack.com]]></googleplay:owner><googleplay:email><![CDATA[singhz@substack.com]]></googleplay:email><googleplay:author><![CDATA[Harpreet Singh]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[Valid Palindrome]]></title><description><![CDATA[Oracle Interview Question]]></description><link>https://singhz.substack.com/p/valid-palindrome-f86</link><guid isPermaLink="false">https://singhz.substack.com/p/valid-palindrome-f86</guid><dc:creator><![CDATA[Harpreet Singh]]></dc:creator><pubDate>Mon, 24 Aug 2026 14:01:07 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!ld7S!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69c20f8-a511-46b8-9ec0-4d6be3002827_1280x720.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!ld7S!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69c20f8-a511-46b8-9ec0-4d6be3002827_1280x720.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!ld7S!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69c20f8-a511-46b8-9ec0-4d6be3002827_1280x720.png 424w, /__u/substackcdn.com/image/fetch/$s_!ld7S!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69c20f8-a511-46b8-9ec0-4d6be3002827_1280x720.png 848w, /__u/substackcdn.com/image/fetch/$s_!ld7S!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69c20f8-a511-46b8-9ec0-4d6be3002827_1280x720.png 1272w, /__u/substackcdn.com/image/fetch/$s_!ld7S!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69c20f8-a511-46b8-9ec0-4d6be3002827_1280x720.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!ld7S!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69c20f8-a511-46b8-9ec0-4d6be3002827_1280x720.png" width="1280" height="720" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b69c20f8-a511-46b8-9ec0-4d6be3002827_1280x720.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:720,&quot;width&quot;:1280,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:307077,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/172145623?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69c20f8-a511-46b8-9ec0-4d6be3002827_1280x720.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!ld7S!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69c20f8-a511-46b8-9ec0-4d6be3002827_1280x720.png 424w, /__u/substackcdn.com/image/fetch/$s_!ld7S!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69c20f8-a511-46b8-9ec0-4d6be3002827_1280x720.png 848w, /__u/substackcdn.com/image/fetch/$s_!ld7S!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69c20f8-a511-46b8-9ec0-4d6be3002827_1280x720.png 1272w, /__u/substackcdn.com/image/fetch/$s_!ld7S!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69c20f8-a511-46b8-9ec0-4d6be3002827_1280x720.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3><strong>Problem Overview</strong></h3><blockquote><p><strong>Difficulty: </strong><span>Easy</span><br><strong>LeetCode Pattern:</strong><span> Two Pointers</span></p></blockquote><p><span>Given a string </span><strong>s</strong><span>, return </span><strong>true</strong><span> if it is a palindrome, or </span><strong>false</strong><span> otherwise.</span></p><p><em>Note: A palindrome is a string that reads the same forward and backward after:</em></p><ul><li><p><em>Converting all uppercase letters to lowercase.</em></p></li><li><p><em>Removing all non-alphanumeric characters (only letters and digits remain).</em></p></li></ul><pre><code><strong>Input:</strong> 
&#183; s = &#8220;race 123 car&#8221;
<strong>Output:</strong> 
&#183; False
<strong>Reason:</strong>
&#183; &#8220;race123car&#8221; is not a palindrome</code></pre><pre><code><strong>Input:</strong> 
&#183; s = &#8220;Race ,: car&#8221;
<strong>Output:</strong> 
&#183; True
<strong>Reason:</strong>
&#183; &#8220;racecar&#8221; is a palindrome</code></pre><div><hr></div><h3><strong>Step 1: Clarify Requirements</strong></h3><ul><li><p><strong>Can the input string be empty?</strong></p><ul><li><p>Yes, empty string is a palindrome.</p></li></ul></li><li><p><strong>Can it contain uppercase letters?</strong></p><ul><li><p>Yes, treat them as lowercase.</p></li></ul></li><li><p><strong>Can it contain non-alphanum chars?</strong></p><ul><li><p>Yes, need to ignore these.</p></li></ul></li></ul><div><hr></div><h3><strong>Step 2: Discuss Approaches</strong></h3><p><strong>1/ Reverse + Compare (Brute Force &#9888;&#65039;)</strong></p><ul><li><p><strong>Logic:</strong></p><ul><li><p>Clean the string:</p><ul><li><p>Remove non-alphanum chars</p></li><li><p>Convert to lowercase</p></li></ul></li><li><p>Make it&#8217;s reversed copy</p></li><li><p>Compare cleaned vs reversed</p></li><li><p>If they match &#8594; return True</p></li><li><p>Else &#8594; return False</p></li></ul></li><li><p><strong>Big O:</strong></p><ul><li><p>Time Complexity: O(n)</p></li><li><p>Space Complexity: O(n)</p></li></ul></li></ul><div><hr></div><p><strong>2/ Two-Pointers (Optimal &#9989;)</strong></p><ul><li><p><strong>Logic:</strong></p><ul><li><p>Use two pointers:</p><ul><li><p>one starting at the left</p></li><li><p>one starting at the right</p></li></ul></li><li><p>At each point:</p><ul><li><p>Skip non-alphanum chars</p></li><li><p>Then, compare left and right</p></li><li><p>If mismatch &#8594; return False</p></li><li><p>Else &#8594; move inward</p></li></ul></li><li><p>If all pairs match &#8594; return True</p></li></ul></li><li><p><strong>Big O:</strong></p><ul><li><p>Time Complexity: O(n)</p></li><li><p>Space Complexity: O(1)</p></li></ul></li></ul><div><hr></div><h3><strong>Step 3: Write Code</strong></h3><h4><strong>Python</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!nMme!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4127ed18-971d-41ff-af40-6fc04210321f_1082x712.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!nMme!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4127ed18-971d-41ff-af40-6fc04210321f_1082x712.png 424w, /__u/substackcdn.com/image/fetch/$s_!nMme!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4127ed18-971d-41ff-af40-6fc04210321f_1082x712.png 848w, /__u/substackcdn.com/image/fetch/$s_!nMme!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4127ed18-971d-41ff-af40-6fc04210321f_1082x712.png 1272w, /__u/substackcdn.com/image/fetch/$s_!nMme!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4127ed18-971d-41ff-af40-6fc04210321f_1082x712.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!nMme!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4127ed18-971d-41ff-af40-6fc04210321f_1082x712.png" width="1082" height="712" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4127ed18-971d-41ff-af40-6fc04210321f_1082x712.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:712,&quot;width&quot;:1082,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:103550,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/172145623?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4127ed18-971d-41ff-af40-6fc04210321f_1082x712.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!nMme!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4127ed18-971d-41ff-af40-6fc04210321f_1082x712.png 424w, /__u/substackcdn.com/image/fetch/$s_!nMme!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4127ed18-971d-41ff-af40-6fc04210321f_1082x712.png 848w, /__u/substackcdn.com/image/fetch/$s_!nMme!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4127ed18-971d-41ff-af40-6fc04210321f_1082x712.png 1272w, /__u/substackcdn.com/image/fetch/$s_!nMme!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4127ed18-971d-41ff-af40-6fc04210321f_1082x712.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4><strong>Java</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!eqWy!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3cdd3072-5207-478e-a601-c1f0be58fcad_1688x904.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!eqWy!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3cdd3072-5207-478e-a601-c1f0be58fcad_1688x904.png 424w, /__u/substackcdn.com/image/fetch/$s_!eqWy!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3cdd3072-5207-478e-a601-c1f0be58fcad_1688x904.png 848w, /__u/substackcdn.com/image/fetch/$s_!eqWy!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3cdd3072-5207-478e-a601-c1f0be58fcad_1688x904.png 1272w, /__u/substackcdn.com/image/fetch/$s_!eqWy!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3cdd3072-5207-478e-a601-c1f0be58fcad_1688x904.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!eqWy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3cdd3072-5207-478e-a601-c1f0be58fcad_1688x904.png" width="1456" height="780" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3cdd3072-5207-478e-a601-c1f0be58fcad_1688x904.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:780,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:158428,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/172145623?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3cdd3072-5207-478e-a601-c1f0be58fcad_1688x904.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!eqWy!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3cdd3072-5207-478e-a601-c1f0be58fcad_1688x904.png 424w, /__u/substackcdn.com/image/fetch/$s_!eqWy!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3cdd3072-5207-478e-a601-c1f0be58fcad_1688x904.png 848w, /__u/substackcdn.com/image/fetch/$s_!eqWy!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3cdd3072-5207-478e-a601-c1f0be58fcad_1688x904.png 1272w, /__u/substackcdn.com/image/fetch/$s_!eqWy!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3cdd3072-5207-478e-a601-c1f0be58fcad_1688x904.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4><strong>C++</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!fHWV!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc09f919d-2334-494e-a81f-0e9e126a7f92_1034x898.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!fHWV!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc09f919d-2334-494e-a81f-0e9e126a7f92_1034x898.png 424w, /__u/substackcdn.com/image/fetch/$s_!fHWV!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc09f919d-2334-494e-a81f-0e9e126a7f92_1034x898.png 848w, /__u/substackcdn.com/image/fetch/$s_!fHWV!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc09f919d-2334-494e-a81f-0e9e126a7f92_1034x898.png 1272w, /__u/substackcdn.com/image/fetch/$s_!fHWV!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc09f919d-2334-494e-a81f-0e9e126a7f92_1034x898.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!fHWV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc09f919d-2334-494e-a81f-0e9e126a7f92_1034x898.png" width="1034" height="898" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c09f919d-2334-494e-a81f-0e9e126a7f92_1034x898.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:898,&quot;width&quot;:1034,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:112223,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/172145623?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc09f919d-2334-494e-a81f-0e9e126a7f92_1034x898.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!fHWV!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc09f919d-2334-494e-a81f-0e9e126a7f92_1034x898.png 424w, /__u/substackcdn.com/image/fetch/$s_!fHWV!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc09f919d-2334-494e-a81f-0e9e126a7f92_1034x898.png 848w, /__u/substackcdn.com/image/fetch/$s_!fHWV!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc09f919d-2334-494e-a81f-0e9e126a7f92_1034x898.png 1272w, /__u/substackcdn.com/image/fetch/$s_!fHWV!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc09f919d-2334-494e-a81f-0e9e126a7f92_1034x898.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h3><strong>Step 4: Answer Follow-Ups</strong></h3><ul><li><p><strong>What if the string is empty?</strong></p><ul><li><p>Our algorithm still works.</p></li><li><p>It will return True as expected.</p></li></ul></li><li><p><strong>What if want case-sensitive check?</strong></p><ul><li><p>Just don&#8217;t convert to lowercase.</p></li><li><p>Rest of algorithm stays the same.</p></li></ul></li><li><p><strong>What if we need all chars check?</strong></p><ul><li><p>Don&#8217;t skip non alphanum chars.</p></li><li><p>Rest of algorithm stays the same.</p></li></ul></li></ul>]]></content:encoded></item><item><title><![CDATA[14 LeetCode Patterns To Crack Google]]></title><description><![CDATA[Google Interview Roadmap]]></description><link>https://singhz.substack.com/p/14-leetcode-patterns-to-crack-google-295</link><guid isPermaLink="false">https://singhz.substack.com/p/14-leetcode-patterns-to-crack-google-295</guid><dc:creator><![CDATA[Harpreet Singh]]></dc:creator><pubDate>Mon, 10 Aug 2026 14:01:46 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!kBgd!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a97efbf-f36b-4aba-89d6-cabb63117267_1264x843.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!kBgd!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a97efbf-f36b-4aba-89d6-cabb63117267_1264x843.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!kBgd!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a97efbf-f36b-4aba-89d6-cabb63117267_1264x843.png 424w, /__u/substackcdn.com/image/fetch/$s_!kBgd!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a97efbf-f36b-4aba-89d6-cabb63117267_1264x843.png 848w, /__u/substackcdn.com/image/fetch/$s_!kBgd!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a97efbf-f36b-4aba-89d6-cabb63117267_1264x843.png 1272w, /__u/substackcdn.com/image/fetch/$s_!kBgd!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a97efbf-f36b-4aba-89d6-cabb63117267_1264x843.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!kBgd!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a97efbf-f36b-4aba-89d6-cabb63117267_1264x843.png" width="1264" height="843" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7a97efbf-f36b-4aba-89d6-cabb63117267_1264x843.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:843,&quot;width&quot;:1264,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1746525,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/210371125?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a97efbf-f36b-4aba-89d6-cabb63117267_1264x843.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!kBgd!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a97efbf-f36b-4aba-89d6-cabb63117267_1264x843.png 424w, /__u/substackcdn.com/image/fetch/$s_!kBgd!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a97efbf-f36b-4aba-89d6-cabb63117267_1264x843.png 848w, /__u/substackcdn.com/image/fetch/$s_!kBgd!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a97efbf-f36b-4aba-89d6-cabb63117267_1264x843.png 1272w, /__u/substackcdn.com/image/fetch/$s_!kBgd!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a97efbf-f36b-4aba-89d6-cabb63117267_1264x843.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Here are <strong>14 LeetCode Patterns</strong> I will study to crack my <strong>Google</strong> interview.</p><p>And no, I won&#8217;t &#8220;grind 500 problems.&#8221;</p><p>I will focus on the right patterns and the right problems.</p><p>These are the<strong> patterns and</strong> <strong>problems</strong> I will practice right before my <strong>Google</strong> interview:</p><div><hr></div><h3><strong>1/ Arrays &amp; Hashing</strong></h3><ol><li><p><a href="https://leetcode.com/problems/contains-duplicate/">Contains Duplicate</a></p></li><li><p><a href="https://leetcode.com/problems/valid-anagram/">Valid Anagram</a></p></li><li><p><a href="https://leetcode.com/problems/two-sum/">Two Sum</a></p></li><li><p><a href="https://leetcode.com/problems/group-anagrams/">Group Anagrams</a></p></li><li><p><a href="https://leetcode.com/problems/longest-consecutive-sequence/">Longest Consecutive Sequence</a></p></li><li><p><a href="https://leetcode.com/problems/valid-sudoku/">Valid Sudoku</a></p></li></ol><h3><strong>2/ Two Pointers</strong></h3><ol><li><p><a href="https://leetcode.com/problems/valid-palindrome/">Valid Palindrome</a></p></li><li><p><a href="https://leetcode.com/problems/remove-duplicates-from-sorted-array/">Remove Duplicates from Sorted Array</a></p></li><li><p><a href="https://leetcode.com/problems/merge-sorted-array/">Merge Sorted Array</a></p></li><li><p><a href="https://leetcode.com/problems/container-with-most-water/">Container With Most Water</a></p></li><li><p><a href="https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/">Two Sum II - Input Array Is Sorted</a></p></li><li><p><a href="https://leetcode.com/problems/3sum/">3Sum</a></p></li></ol><h3><strong>3/ Stack</strong></h3><ol><li><p><a href="https://leetcode.com/problems/valid-parentheses/">Valid Parentheses</a></p></li><li><p><a href="https://leetcode.com/problems/next-greater-element-i/">Next Greater Element I</a></p></li><li><p><a href="https://leetcode.com/problems/remove-outermost-parentheses/">Remove Outermost Parentheses</a></p></li><li><p><a href="https://leetcode.com/problems/min-stack/">Min Stack</a></p></li><li><p><a href="https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/">Minimum Add to Make Parentheses Valid</a></p></li><li><p><a href="https://leetcode.com/problems/basic-calculator-ii/">Basic Calculator II</a></p></li></ol><h3><strong>4/ Binary Search</strong></h3><ol><li><p><a href="https://leetcode.com/problems/binary-search/">Binary Search</a></p></li><li><p><a href="https://leetcode.com/problems/search-insert-position/">Search Insert Position</a></p></li><li><p><a href="https://leetcode.com/problems/kth-missing-positive-number/">Kth Missing Positive Number</a></p></li><li><p><a href="https://leetcode.com/problems/find-peak-element/">Find Peak Element</a></p></li><li><p><a href="https://leetcode.com/problems/search-in-rotated-sorted-array/">Search in Rotated Sorted Array</a></p></li><li><p><a href="https://leetcode.com/problems/search-a-2d-matrix/">Search a 2D Matrix</a></p></li></ol><h3><strong>5/ Sliding Window</strong></h3><ol><li><p><a href="https://leetcode.com/problems/maximum-average-subarray-i/">Maximum Average Subarray I</a></p></li><li><p><a href="https://leetcode.com/problems/find-the-k-beauty-of-a-number/">Find the K-Beauty of a Number</a></p></li><li><p><a href="https://leetcode.com/problems/contains-duplicate-ii/">Contains Duplicate II</a></p></li><li><p><a href="https://leetcode.com/problems/longest-substring-without-repeating-characters/">Longest Substring Without Repeating Characters</a></p></li><li><p><a href="https://leetcode.com/problems/subarray-product-less-than-k/">Subarray Product Less Than K</a></p></li><li><p><a href="https://leetcode.com/problems/longest-repeating-character-replacement/">Longest Repeating Character Replacement</a></p></li></ol><h3><strong>6/ Linked List</strong></h3><ol><li><p><a href="https://leetcode.com/problems/merge-two-sorted-lists/">Merge Two Sorted Lists</a></p></li><li><p><a href="https://leetcode.com/problems/reverse-linked-list/">Reverse Linked List</a></p></li><li><p><a href="https://leetcode.com/problems/middle-of-the-linked-list/">Middle of the Linked List</a></p></li><li><p><a href="https://leetcode.com/problems/add-two-numbers/">Add Two Numbers</a></p></li><li><p><a href="https://leetcode.com/problems/lru-cache/">LRU Cache</a></p></li><li><p><a href="https://leetcode.com/problems/reorder-list/">Reorder List</a></p></li></ol><h3><strong>7/ Trees</strong></h3><ol><li><p><a href="https://leetcode.com/problems/same-tree/">Same Tree</a></p></li><li><p><a href="https://leetcode.com/problems/maximum-depth-of-binary-tree/">Maximum Depth of Binary Tree</a></p></li><li><p><a href="https://leetcode.com/problems/diameter-of-binary-tree/">Diameter of Binary Tree</a></p></li><li><p><a href="https://leetcode.com/problems/binary-tree-level-order-traversal/">Binary Tree Level Order Traversal</a></p></li><li><p><a href="https://leetcode.com/problems/validate-binary-search-tree/">Validate Binary Search Tree</a></p></li><li><p><a href="https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/">Lowest Common Ancestor of a Binary Tree</a></p></li></ol><h3><strong>8/ Trie</strong></h3><ol><li><p><a href="https://leetcode.com/problems/longest-common-prefix/">Longest Common Prefix</a></p></li><li><p><a href="https://leetcode.com/problems/count-prefix-and-suffix-pairs-i/">Count Prefix and Suffix Pairs I</a></p></li><li><p><a href="https://leetcode.com/problems/implement-trie-prefix-tree/">Implement Trie (Prefix Tree)</a></p></li><li><p><a href="https://leetcode.com/problems/design-add-and-search-words-data-structure/">Design Add And Search Words Data Structure</a></p></li><li><p><a href="https://leetcode.com/problems/find-the-length-of-the-longest-common-prefix/">Find the Length of the Longest Common Prefix</a></p></li></ol><h3><strong>9/ Backtracking</strong></h3><ol><li><p><a href="https://leetcode.com/problems/binary-tree-paths/">Binary Tree Paths</a></p></li><li><p><a href="https://leetcode.com/problems/binary-watch/">Binary Watch</a></p></li><li><p><a href="https://leetcode.com/problems/sum-of-all-subset-xor-totals/">Sum of All Subset XOR Totals</a></p></li><li><p><a href="https://leetcode.com/problems/subsets/">Subsets</a></p></li><li><p><a href="https://leetcode.com/problems/permutations/">Permutations</a></p></li><li><p><a href="https://leetcode.com/problems/combination-sum/">Combination Sum</a></p></li></ol><h3><strong>10/ Heap (Priority Queue)</strong></h3><ol><li><p><a href="https://leetcode.com/problems/last-stone-weight/">Last Stone Weight</a></p></li><li><p><a href="https://leetcode.com/problems/kth-largest-element-in-a-stream/">Kth Largest Element in a Stream</a></p></li><li><p><a href="https://leetcode.com/problems/relative-ranks/">Relative Ranks</a></p></li><li><p><a href="https://leetcode.com/problems/k-closest-points-to-origin/">K Closest Points to Origin</a></p></li><li><p><a href="https://leetcode.com/problems/kth-largest-element-in-an-array/">Kth Largest Element in an Array</a></p></li><li><p><a href="https://leetcode.com/problems/task-scheduler/">Task Scheduler</a></p></li></ol><h3><strong>11/ Graphs</strong></h3><ol><li><p><a href="https://leetcode.com/problems/find-center-of-star-graph/">Find Center of Star Graph</a></p></li><li><p><a href="https://leetcode.com/problems/find-the-town-judge/">Find the Town Judge</a></p></li><li><p><a href="https://leetcode.com/problems/find-if-path-exists-in-graph/">Find if Path Exists in Graph</a></p></li><li><p><a href="https://leetcode.com/problems/number-of-islands/">Number of Islands</a></p></li><li><p><a href="https://leetcode.com/problems/clone-graph/">Clone Graph</a></p></li><li><p><a href="https://leetcode.com/problems/rotting-oranges/">Rotting Oranges</a></p></li></ol><h3><strong>12/ Intervals</strong></h3><ol><li><p><a href="https://leetcode.com/problems/insert-interval/">Insert Interval</a></p></li><li><p><a href="https://leetcode.com/problems/merge-intervals/">Merge Intervals</a></p></li><li><p><a href="https://leetcode.com/problems/non-overlapping-intervals/">Non Overlapping Intervals</a></p></li></ol><h3><strong>13/ Greedy &amp; Dynamic Programming</strong></h3><ol><li><p><a href="https://leetcode.com/problems/jump-game/">Jump Game</a></p></li><li><p><a href="https://leetcode.com/problems/jump-game-ii/">Jump Game II</a></p></li><li><p><a href="https://leetcode.com/problems/gas-station/">Gas Station</a></p></li></ol><h3><strong>14/ Advanced Graphs</strong></h3><ol><li><p><a href="https://leetcode.com/problems/cheapest-flights-within-k-stops/">Cheapest Flights Within K Stops</a></p></li><li><p><a href="https://leetcode.com/problems/network-delay-time/">Network Delay Time</a></p></li><li><p><a href="https://leetcode.com/problems/min-cost-to-connect-all-points/">Min Cost to Connect All Points</a></p></li></ol><div><hr></div><p>If you&#8217;re preparing for FAANG in 2026, don&#8217;t forget to save this post.</p><p>Also, here&#8217;s a platform which you can use to practice for your interviews:</p><p><strong>&#128279; Interview Roadmap Platform:</strong> <a href="https://interview-roadmap.vercel.app/">Link</a></p><p>Best of luck!</p>]]></content:encoded></item><item><title><![CDATA[Valid Sudoku]]></title><description><![CDATA[Uber Interview Question]]></description><link>https://singhz.substack.com/p/valid-sudoku-e9a</link><guid isPermaLink="false">https://singhz.substack.com/p/valid-sudoku-e9a</guid><dc:creator><![CDATA[Harpreet Singh]]></dc:creator><pubDate>Mon, 03 Aug 2026 14:02:36 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!lu0F!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F620f66c2-8e67-404b-a128-41eb2ee63c27_1280x720.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!lu0F!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F620f66c2-8e67-404b-a128-41eb2ee63c27_1280x720.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!lu0F!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F620f66c2-8e67-404b-a128-41eb2ee63c27_1280x720.png 424w, /__u/substackcdn.com/image/fetch/$s_!lu0F!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F620f66c2-8e67-404b-a128-41eb2ee63c27_1280x720.png 848w, /__u/substackcdn.com/image/fetch/$s_!lu0F!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F620f66c2-8e67-404b-a128-41eb2ee63c27_1280x720.png 1272w, /__u/substackcdn.com/image/fetch/$s_!lu0F!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F620f66c2-8e67-404b-a128-41eb2ee63c27_1280x720.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!lu0F!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F620f66c2-8e67-404b-a128-41eb2ee63c27_1280x720.png" width="1280" height="720" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/620f66c2-8e67-404b-a128-41eb2ee63c27_1280x720.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:720,&quot;width&quot;:1280,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:302932,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/172145163?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F620f66c2-8e67-404b-a128-41eb2ee63c27_1280x720.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!lu0F!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F620f66c2-8e67-404b-a128-41eb2ee63c27_1280x720.png 424w, /__u/substackcdn.com/image/fetch/$s_!lu0F!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F620f66c2-8e67-404b-a128-41eb2ee63c27_1280x720.png 848w, /__u/substackcdn.com/image/fetch/$s_!lu0F!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F620f66c2-8e67-404b-a128-41eb2ee63c27_1280x720.png 1272w, /__u/substackcdn.com/image/fetch/$s_!lu0F!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F620f66c2-8e67-404b-a128-41eb2ee63c27_1280x720.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3><strong>Problem Overview</strong></h3><blockquote><p><strong>Difficulty: </strong>Medium<br><strong>LeetCode Pattern:</strong> Arrays &amp; Hashing</p></blockquote><p>Determine if a <strong>9 x 9</strong> Sudoku board is valid according to the following rules:</p><ol><li><p>Each row must contain the digits <strong>1-9</strong> without repetition.</p></li><li><p>Each column must contain the digits <strong>1-9</strong> without repetition.</p></li><li><p>Each of the nine 3 x 3 boxes of the grid must contain the digits <strong>1-9</strong> without repetition.</p></li></ol><p><em>Note:</em></p><ul><li><p><em>Only the filled cells need to be validated according to the mentioned rules.</em></p></li><li><p><em>For unfilled cells in the matrix, assume it would be the dot (.) character.</em></p></li></ul><p><strong>Example 1</strong></p><ul><li><p>Valid Case.</p></li><li><p>It satisfies all properties.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!mEVX!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23804f98-b0cc-4ab5-a4f4-300f1406f709_972x946.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!mEVX!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23804f98-b0cc-4ab5-a4f4-300f1406f709_972x946.png 424w, /__u/substackcdn.com/image/fetch/$s_!mEVX!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23804f98-b0cc-4ab5-a4f4-300f1406f709_972x946.png 848w, /__u/substackcdn.com/image/fetch/$s_!mEVX!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23804f98-b0cc-4ab5-a4f4-300f1406f709_972x946.png 1272w, /__u/substackcdn.com/image/fetch/$s_!mEVX!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23804f98-b0cc-4ab5-a4f4-300f1406f709_972x946.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!mEVX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23804f98-b0cc-4ab5-a4f4-300f1406f709_972x946.png" width="972" height="946" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/23804f98-b0cc-4ab5-a4f4-300f1406f709_972x946.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:946,&quot;width&quot;:972,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:59171,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/163664134?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff911f29f-034b-438c-949b-04dbbf764b33_972x962.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!mEVX!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23804f98-b0cc-4ab5-a4f4-300f1406f709_972x946.png 424w, /__u/substackcdn.com/image/fetch/$s_!mEVX!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23804f98-b0cc-4ab5-a4f4-300f1406f709_972x946.png 848w, /__u/substackcdn.com/image/fetch/$s_!mEVX!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23804f98-b0cc-4ab5-a4f4-300f1406f709_972x946.png 1272w, /__u/substackcdn.com/image/fetch/$s_!mEVX!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23804f98-b0cc-4ab5-a4f4-300f1406f709_972x946.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><strong>Example 2</strong></p><ul><li><p>Invalid Case.</p></li><li><p>Two 8's in the top left 3x3 box.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Rk8n!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e9b80cb-3b50-4323-9f3e-be9d5a89470d_952x943.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Rk8n!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e9b80cb-3b50-4323-9f3e-be9d5a89470d_952x943.png 424w, /__u/substackcdn.com/image/fetch/$s_!Rk8n!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e9b80cb-3b50-4323-9f3e-be9d5a89470d_952x943.png 848w, /__u/substackcdn.com/image/fetch/$s_!Rk8n!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e9b80cb-3b50-4323-9f3e-be9d5a89470d_952x943.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Rk8n!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e9b80cb-3b50-4323-9f3e-be9d5a89470d_952x943.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Rk8n!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e9b80cb-3b50-4323-9f3e-be9d5a89470d_952x943.png" width="952" height="943" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9e9b80cb-3b50-4323-9f3e-be9d5a89470d_952x943.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:943,&quot;width&quot;:952,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:64882,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/163664134?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1287a28b-1ceb-4bf0-aab4-7c66dbe9fb92_952x960.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!Rk8n!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e9b80cb-3b50-4323-9f3e-be9d5a89470d_952x943.png 424w, /__u/substackcdn.com/image/fetch/$s_!Rk8n!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e9b80cb-3b50-4323-9f3e-be9d5a89470d_952x943.png 848w, /__u/substackcdn.com/image/fetch/$s_!Rk8n!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e9b80cb-3b50-4323-9f3e-be9d5a89470d_952x943.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Rk8n!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e9b80cb-3b50-4323-9f3e-be9d5a89470d_952x943.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h3><strong>Step 1: Clarify Requirements</strong></h3><ul><li><p><strong>Can the board have all empty cells?</strong></p><ul><li><p>Yes, that&#8217;s possible.</p></li></ul></li><li><p><strong>Can there by values other than 1-9?</strong></p><ul><li><p>No, assume values are 1-9 only.</p></li></ul></li><li><p><strong>Can board size be other than 9x9?</strong></p><ul><li><p>No, assume 9x9 board only.</p></li></ul></li></ul><div><hr></div><h3><strong>Step 2: Discuss Approaches</strong></h3><p><strong>1/ All Checks (Brute Force &#9888;&#65039;)</strong></p><ul><li><p><strong>Logic:</strong></p><ul><li><p>For each filled cell:</p><ul><li><p>Check every value in that row</p></li><li><p>Check every value in that col</p></li><li><p>Check every value in that box</p></li></ul></li><li><p>If no match &#8594; return True</p></li><li><p>If match found &#8594; return False</p></li></ul></li><li><p><strong>Big O:</strong></p><ul><li><p>Time Complexity: O(n<sup>3</sup>)</p></li><li><p>Space Complexity: O(1)</p></li></ul></li></ul><div><hr></div><p><strong>2/ Use Multiple Sets (Optimal &#9989;)</strong></p><ul><li><p><strong>Logic:</strong></p><ul><li><p>Create 27 sets in total:</p><ul><li><p>9 for rows</p></li><li><p>9 for cols</p></li><li><p>9 for boxes</p></li></ul></li><li><p>For each filled cell:</p><ul><li><p>Check value in:</p><ul><li><p>row set</p></li><li><p>col set</p></li><li><p>box set</p></li></ul></li><li><p>If value exists &#8594; return False</p></li><li><p>Else &#8594; add to sets and continue</p></li></ul></li><li><p>At the end:</p><ul><li><p>return True (valid board)</p></li></ul></li></ul></li><li><p><strong>Big O:</strong></p><ul><li><p>Time Complexity: O(n<sup>2</sup>)</p></li><li><p>Space Complexity: O(1)</p></li></ul></li></ul><div><hr></div><h3><strong>Step 3: Write Code</strong></h3><h4><strong>Python</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!nSwy!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4061f91-fb9c-4efe-ab9f-11b752c5d49d_1480x1190.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!nSwy!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4061f91-fb9c-4efe-ab9f-11b752c5d49d_1480x1190.jpeg 424w, /__u/substackcdn.com/image/fetch/$s_!nSwy!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4061f91-fb9c-4efe-ab9f-11b752c5d49d_1480x1190.jpeg 848w, /__u/substackcdn.com/image/fetch/$s_!nSwy!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4061f91-fb9c-4efe-ab9f-11b752c5d49d_1480x1190.jpeg 1272w, /__u/substackcdn.com/image/fetch/$s_!nSwy!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4061f91-fb9c-4efe-ab9f-11b752c5d49d_1480x1190.jpeg 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!nSwy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4061f91-fb9c-4efe-ab9f-11b752c5d49d_1480x1190.jpeg" width="1456" height="1171" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b4061f91-fb9c-4efe-ab9f-11b752c5d49d_1480x1190.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1171,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:83471,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/172145163?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4061f91-fb9c-4efe-ab9f-11b752c5d49d_1480x1190.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!nSwy!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4061f91-fb9c-4efe-ab9f-11b752c5d49d_1480x1190.jpeg 424w, /__u/substackcdn.com/image/fetch/$s_!nSwy!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4061f91-fb9c-4efe-ab9f-11b752c5d49d_1480x1190.jpeg 848w, /__u/substackcdn.com/image/fetch/$s_!nSwy!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4061f91-fb9c-4efe-ab9f-11b752c5d49d_1480x1190.jpeg 1272w, /__u/substackcdn.com/image/fetch/$s_!nSwy!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4061f91-fb9c-4efe-ab9f-11b752c5d49d_1480x1190.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4><strong>Java</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!TJl_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F066e4446-6d94-4277-863f-ab8cc2d1564e_1714x1314.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!TJl_!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F066e4446-6d94-4277-863f-ab8cc2d1564e_1714x1314.png 424w, /__u/substackcdn.com/image/fetch/$s_!TJl_!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F066e4446-6d94-4277-863f-ab8cc2d1564e_1714x1314.png 848w, /__u/substackcdn.com/image/fetch/$s_!TJl_!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F066e4446-6d94-4277-863f-ab8cc2d1564e_1714x1314.png 1272w, /__u/substackcdn.com/image/fetch/$s_!TJl_!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F066e4446-6d94-4277-863f-ab8cc2d1564e_1714x1314.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!TJl_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F066e4446-6d94-4277-863f-ab8cc2d1564e_1714x1314.png" width="1456" height="1116" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/066e4446-6d94-4277-863f-ab8cc2d1564e_1714x1314.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1116,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:264089,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/172145163?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F066e4446-6d94-4277-863f-ab8cc2d1564e_1714x1314.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!TJl_!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F066e4446-6d94-4277-863f-ab8cc2d1564e_1714x1314.png 424w, /__u/substackcdn.com/image/fetch/$s_!TJl_!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F066e4446-6d94-4277-863f-ab8cc2d1564e_1714x1314.png 848w, /__u/substackcdn.com/image/fetch/$s_!TJl_!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F066e4446-6d94-4277-863f-ab8cc2d1564e_1714x1314.png 1272w, /__u/substackcdn.com/image/fetch/$s_!TJl_!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F066e4446-6d94-4277-863f-ab8cc2d1564e_1714x1314.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4><strong>C++</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!rJej!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6365299d-f46b-4170-a17b-d74da47c7f8e_1584x984.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!rJej!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6365299d-f46b-4170-a17b-d74da47c7f8e_1584x984.png 424w, /__u/substackcdn.com/image/fetch/$s_!rJej!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6365299d-f46b-4170-a17b-d74da47c7f8e_1584x984.png 848w, /__u/substackcdn.com/image/fetch/$s_!rJej!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6365299d-f46b-4170-a17b-d74da47c7f8e_1584x984.png 1272w, /__u/substackcdn.com/image/fetch/$s_!rJej!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6365299d-f46b-4170-a17b-d74da47c7f8e_1584x984.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!rJej!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6365299d-f46b-4170-a17b-d74da47c7f8e_1584x984.png" width="1456" height="904" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6365299d-f46b-4170-a17b-d74da47c7f8e_1584x984.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:904,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:173115,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/172145163?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6365299d-f46b-4170-a17b-d74da47c7f8e_1584x984.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!rJej!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6365299d-f46b-4170-a17b-d74da47c7f8e_1584x984.png 424w, /__u/substackcdn.com/image/fetch/$s_!rJej!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6365299d-f46b-4170-a17b-d74da47c7f8e_1584x984.png 848w, /__u/substackcdn.com/image/fetch/$s_!rJej!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6365299d-f46b-4170-a17b-d74da47c7f8e_1584x984.png 1272w, /__u/substackcdn.com/image/fetch/$s_!rJej!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6365299d-f46b-4170-a17b-d74da47c7f8e_1584x984.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h3><strong>Step 4: Answer Follow-Ups</strong></h3><ul><li><p><strong>What if the board was nearly empty?</strong></p><ul><li><p>Our algorithm still works fine.</p></li><li><p>It will simply skip most cells.</p></li></ul></li><li><p><strong>What if board contains invalid chars?</strong></p><ul><li><p>We can add validation checks.</p></li><li><p>If an invalid char is found:</p><ul><li><p>Return False (invalid board), or</p></li><li><p>Throw an error (bad input).</p></li></ul></li></ul></li><li><p><strong>What if the board size was any nxn?</strong></p><ul><li><p>Replace hardcoded 9 with n.</p></li><li><p>Box Size is now: sqrt(n) &#215; sqrt(n).</p></li><li><p>Rest of algorithm stays the same.</p></li><li><p>Big O remains the same as well.</p></li></ul></li></ul>]]></content:encoded></item><item><title><![CDATA[Group Anagrams]]></title><description><![CDATA[Bloomberg Interview Question]]></description><link>https://singhz.substack.com/p/group-anagrams-76a</link><guid isPermaLink="false">https://singhz.substack.com/p/group-anagrams-76a</guid><dc:creator><![CDATA[Harpreet Singh]]></dc:creator><pubDate>Mon, 27 Jul 2026 17:22:54 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!D6wj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0ea9077c-607b-4a2b-b267-6295810be0a6_1280x720.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!D6wj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0ea9077c-607b-4a2b-b267-6295810be0a6_1280x720.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!D6wj!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0ea9077c-607b-4a2b-b267-6295810be0a6_1280x720.png 424w, /__u/substackcdn.com/image/fetch/$s_!D6wj!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0ea9077c-607b-4a2b-b267-6295810be0a6_1280x720.png 848w, /__u/substackcdn.com/image/fetch/$s_!D6wj!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0ea9077c-607b-4a2b-b267-6295810be0a6_1280x720.png 1272w, /__u/substackcdn.com/image/fetch/$s_!D6wj!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0ea9077c-607b-4a2b-b267-6295810be0a6_1280x720.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!D6wj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0ea9077c-607b-4a2b-b267-6295810be0a6_1280x720.png" width="1280" height="720" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0ea9077c-607b-4a2b-b267-6295810be0a6_1280x720.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:720,&quot;width&quot;:1280,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:309844,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/172144316?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0ea9077c-607b-4a2b-b267-6295810be0a6_1280x720.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!D6wj!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0ea9077c-607b-4a2b-b267-6295810be0a6_1280x720.png 424w, /__u/substackcdn.com/image/fetch/$s_!D6wj!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0ea9077c-607b-4a2b-b267-6295810be0a6_1280x720.png 848w, /__u/substackcdn.com/image/fetch/$s_!D6wj!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0ea9077c-607b-4a2b-b267-6295810be0a6_1280x720.png 1272w, /__u/substackcdn.com/image/fetch/$s_!D6wj!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0ea9077c-607b-4a2b-b267-6295810be0a6_1280x720.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3><strong>Problem Overview</strong></h3><blockquote><p><strong>Difficulty: </strong>Medium<br><strong>LeetCode Pattern:</strong> Arrays &amp; Hashing</p></blockquote><p>Given an array of strings, group the anagrams together and return them. You can return the answer in any order.</p><p><em>Note: Anagram is a word or phrase formed by rearranging the letters of another word or phrase.</em></p><pre><code><strong>Input:</strong> 
&#183; ["eat","tan","nat"]
<strong>Output:</strong> 
&#183; [["eat"],["nat","tan"]]</code></pre><pre><code><strong>Input:</strong> 
&#183; ["eat","tan","bat"]
<strong>Output:</strong> 
&#183; [["eat"],["tan"],["bat"]]</code></pre><div><hr></div><h3><strong>Step 1: Clarify Requirements</strong></h3><ul><li><p><strong>Can input array be empty?</strong></p><ul><li><p>Yes, return an empty array.</p></li></ul></li><li><p><strong>Can strings have uppercase letters?</strong></p><ul><li><p>No, assume lowercase letters only.</p></li></ul></li><li><p><strong>Can there be empty strings?</strong></p><ul><li><p>Yes, put them into the same group.</p></li></ul></li></ul><div><hr></div><h3><strong>Step 2: Discuss Approaches</strong></h3><p><strong>1/ Sort + Map (Brute Force &#9888;&#65039;)</strong></p><ul><li><p><strong>Logic:</strong></p><ul><li><p>For each string:</p><ul><li><p>Sort it first</p></li><li><p>Then, put it in the map:</p><ul><li><p>key = sorted string</p></li><li><p>value = [original string]</p></li></ul></li></ul></li></ul></li><li><p><strong>Big O:</strong></p><ul><li><p>Time Complexity: O(n&#183;klogk)</p></li><li><p>Space Complexity: O(n&#183;k)</p></li></ul></li></ul><div><hr></div><p><strong>2/ Use Count Arrays (Optimal &#9989;)</strong></p><ul><li><p><strong>Idea:</strong></p><ul><li><p>Sorting each string takes time</p></li><li><p>But we don&#8217;t need to sort</p></li><li><p>We assume strings only contain:</p><ul><li><p>26 unique chars</p></li><li><p>Lowercase letters only</p></li></ul></li><li><p>So instead of sorting:</p><ul><li><p>We use a fixed-size array</p></li><li><p>To keep char counts</p></li></ul></li></ul></li><li><p><strong>Logic:</strong></p><ul><li><p>For each string:</p><ul><li><p>Initialize a fixed-size array</p></li><li><p>Keep char counts in it</p></li><li><p>Convert char counts to a key</p></li><li><p>Put it in the map:</p><ul><li><p>Key = char counts string</p></li><li><p>Value = [original string]</p></li></ul></li></ul></li><li><p>At the end:</p><ul><li><p>Return map values (lists) as a list</p></li></ul></li></ul></li><li><p><strong>Big O:</strong></p><ul><li><p>Time Complexity: O(n&#183;k)</p></li><li><p>Space Complexity: O(n&#183;k)</p></li></ul></li></ul><div><hr></div><h3><strong>Step 3: Write Code</strong></h3><h4><strong>Python</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!wuXU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92f65cc8-2515-4bc8-8daa-2b8b04722cd2_1182x894.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!wuXU!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92f65cc8-2515-4bc8-8daa-2b8b04722cd2_1182x894.png 424w, /__u/substackcdn.com/image/fetch/$s_!wuXU!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92f65cc8-2515-4bc8-8daa-2b8b04722cd2_1182x894.png 848w, /__u/substackcdn.com/image/fetch/$s_!wuXU!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92f65cc8-2515-4bc8-8daa-2b8b04722cd2_1182x894.png 1272w, /__u/substackcdn.com/image/fetch/$s_!wuXU!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92f65cc8-2515-4bc8-8daa-2b8b04722cd2_1182x894.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!wuXU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92f65cc8-2515-4bc8-8daa-2b8b04722cd2_1182x894.png" width="1182" height="894" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/92f65cc8-2515-4bc8-8daa-2b8b04722cd2_1182x894.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:894,&quot;width&quot;:1182,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:107371,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/172144316?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92f65cc8-2515-4bc8-8daa-2b8b04722cd2_1182x894.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!wuXU!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92f65cc8-2515-4bc8-8daa-2b8b04722cd2_1182x894.png 424w, /__u/substackcdn.com/image/fetch/$s_!wuXU!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92f65cc8-2515-4bc8-8daa-2b8b04722cd2_1182x894.png 848w, /__u/substackcdn.com/image/fetch/$s_!wuXU!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92f65cc8-2515-4bc8-8daa-2b8b04722cd2_1182x894.png 1272w, /__u/substackcdn.com/image/fetch/$s_!wuXU!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92f65cc8-2515-4bc8-8daa-2b8b04722cd2_1182x894.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4><strong>Java</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!C0l2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb8c5880-efb6-4f8f-9f21-06bcf9261d05_1218x1004.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!C0l2!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb8c5880-efb6-4f8f-9f21-06bcf9261d05_1218x1004.png 424w, /__u/substackcdn.com/image/fetch/$s_!C0l2!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb8c5880-efb6-4f8f-9f21-06bcf9261d05_1218x1004.png 848w, /__u/substackcdn.com/image/fetch/$s_!C0l2!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb8c5880-efb6-4f8f-9f21-06bcf9261d05_1218x1004.png 1272w, /__u/substackcdn.com/image/fetch/$s_!C0l2!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb8c5880-efb6-4f8f-9f21-06bcf9261d05_1218x1004.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!C0l2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb8c5880-efb6-4f8f-9f21-06bcf9261d05_1218x1004.png" width="1218" height="1004" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bb8c5880-efb6-4f8f-9f21-06bcf9261d05_1218x1004.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1004,&quot;width&quot;:1218,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:216228,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/172144316?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb8c5880-efb6-4f8f-9f21-06bcf9261d05_1218x1004.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!C0l2!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb8c5880-efb6-4f8f-9f21-06bcf9261d05_1218x1004.png 424w, /__u/substackcdn.com/image/fetch/$s_!C0l2!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb8c5880-efb6-4f8f-9f21-06bcf9261d05_1218x1004.png 848w, /__u/substackcdn.com/image/fetch/$s_!C0l2!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb8c5880-efb6-4f8f-9f21-06bcf9261d05_1218x1004.png 1272w, /__u/substackcdn.com/image/fetch/$s_!C0l2!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb8c5880-efb6-4f8f-9f21-06bcf9261d05_1218x1004.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4><strong>C++</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!WFIl!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2055fd4-f147-4504-8e66-2b01e9a22dcf_1128x1160.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!WFIl!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2055fd4-f147-4504-8e66-2b01e9a22dcf_1128x1160.png 424w, /__u/substackcdn.com/image/fetch/$s_!WFIl!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2055fd4-f147-4504-8e66-2b01e9a22dcf_1128x1160.png 848w, /__u/substackcdn.com/image/fetch/$s_!WFIl!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2055fd4-f147-4504-8e66-2b01e9a22dcf_1128x1160.png 1272w, /__u/substackcdn.com/image/fetch/$s_!WFIl!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2055fd4-f147-4504-8e66-2b01e9a22dcf_1128x1160.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!WFIl!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2055fd4-f147-4504-8e66-2b01e9a22dcf_1128x1160.png" width="1128" height="1160" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f2055fd4-f147-4504-8e66-2b01e9a22dcf_1128x1160.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1160,&quot;width&quot;:1128,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:188226,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/172144316?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2055fd4-f147-4504-8e66-2b01e9a22dcf_1128x1160.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!WFIl!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2055fd4-f147-4504-8e66-2b01e9a22dcf_1128x1160.png 424w, /__u/substackcdn.com/image/fetch/$s_!WFIl!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2055fd4-f147-4504-8e66-2b01e9a22dcf_1128x1160.png 848w, /__u/substackcdn.com/image/fetch/$s_!WFIl!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2055fd4-f147-4504-8e66-2b01e9a22dcf_1128x1160.png 1272w, /__u/substackcdn.com/image/fetch/$s_!WFIl!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2055fd4-f147-4504-8e66-2b01e9a22dcf_1128x1160.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h3><strong>Step 4: Answer Follow-Ups</strong></h3><ul><li><p><strong>What if case doesn&#8217;t matter?</strong></p><ul><li><p>Convert all strings to lowercase.</p></li><li><p>Then run the grouping logic.</p></li></ul></li><li><p><strong>What if input contains Unicode?</strong></p><ul><li><p>For Unicode, arrays don&#8217;t work.</p></li><li><p>Use a map to count each char.</p></li><li><p>Sort the characters in the map.</p></li><li><p>Turn that into a key string.</p></li><li><p>Group words with the same key.</p></li></ul></li></ul>]]></content:encoded></item><item><title><![CDATA[Longest Consecutive Sequence]]></title><description><![CDATA[Microsoft Interview Question]]></description><link>https://singhz.substack.com/p/longest-consecutive-sequence-50a</link><guid isPermaLink="false">https://singhz.substack.com/p/longest-consecutive-sequence-50a</guid><dc:creator><![CDATA[Harpreet Singh]]></dc:creator><pubDate>Mon, 13 Jul 2026 14:01:59 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!24K7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72f78bb2-8094-457d-8c57-e3ea3d5de794_1280x720.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!24K7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72f78bb2-8094-457d-8c57-e3ea3d5de794_1280x720.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!24K7!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72f78bb2-8094-457d-8c57-e3ea3d5de794_1280x720.png 424w, /__u/substackcdn.com/image/fetch/$s_!24K7!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72f78bb2-8094-457d-8c57-e3ea3d5de794_1280x720.png 848w, /__u/substackcdn.com/image/fetch/$s_!24K7!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72f78bb2-8094-457d-8c57-e3ea3d5de794_1280x720.png 1272w, /__u/substackcdn.com/image/fetch/$s_!24K7!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72f78bb2-8094-457d-8c57-e3ea3d5de794_1280x720.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!24K7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72f78bb2-8094-457d-8c57-e3ea3d5de794_1280x720.png" width="1280" height="720" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/72f78bb2-8094-457d-8c57-e3ea3d5de794_1280x720.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:720,&quot;width&quot;:1280,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:314316,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/171051986?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72f78bb2-8094-457d-8c57-e3ea3d5de794_1280x720.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!24K7!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72f78bb2-8094-457d-8c57-e3ea3d5de794_1280x720.png 424w, /__u/substackcdn.com/image/fetch/$s_!24K7!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72f78bb2-8094-457d-8c57-e3ea3d5de794_1280x720.png 848w, /__u/substackcdn.com/image/fetch/$s_!24K7!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72f78bb2-8094-457d-8c57-e3ea3d5de794_1280x720.png 1272w, /__u/substackcdn.com/image/fetch/$s_!24K7!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72f78bb2-8094-457d-8c57-e3ea3d5de794_1280x720.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3><strong>Problem Overview</strong></h3><blockquote><p><strong>Difficulty: </strong>Medium<br><strong>LeetCode Pattern:</strong> Arrays &amp; Hashing</p></blockquote><p>Given an unsorted array of integers <strong>nums</strong>, return the length of the longest consecutive elements sequence.</p><pre><code><strong>Input:</strong> 
&#183; nums = [100,4,200,1,3,2]
<strong>Output:</strong> 
&#183; 4
<strong>Explanation:</strong> 
&#183; Longest Seq. = [1, 2, 3, 4]
&#183; It's length is 4</code></pre><div><hr></div><h3><strong>Step 1: Clarify Requirements</strong></h3><ul><li><p><strong>Can input be empty?</strong></p><ul><li><p>Yes, return 0.</p></li></ul></li><li><p><strong>Can input include negatives?</strong></p><ul><li><p>Yes.</p></li></ul></li><li><p><strong>Can input contain duplicates?</strong></p><ul><li><p>Yes.</p></li></ul></li></ul><div><hr></div><h3><strong>Step 2: Discuss Approaches</strong></h3><p><strong>1/ Sort + Linear Scan (Brute Force &#9888;&#65039;)</strong></p><ul><li><p><strong>Logic:</strong></p><ul><li><p>Sort the array</p></li><li><p>Iterate through it</p></li><li><p>Count consecutive sequences</p></li><li><p>Track their lengths</p></li><li><p>Keep max length</p></li></ul></li><li><p><strong>Big O:</strong></p><ul><li><p>Time Complexity: O(n&#183;logn)</p></li><li><p>Space Complexity: O(n)</p></li></ul></li></ul><div><hr></div><p><strong>2/ Set + Linear Scan (Optimal &#9989;)</strong></p><ul><li><p><strong>Logic:</strong></p><ul><li><p>Put all nums in a set (O(1) lookups)</p></li><li><p>For each num in set:</p><ul><li><p>Check if <em>num-1</em> is not in set</p></li><li><p>If not, it&#8217;s start of a sequence</p></li></ul></li><li><p>For each sequence start:</p><ul><li><p>Count consecutive numbers</p></li><li><p>And keep track of max length</p></li></ul></li></ul></li><li><p><strong>Big O:</strong></p><ul><li><p>Time Complexity: O(n)</p></li><li><p>Space Complexity: O(n)</p></li></ul></li></ul><div><hr></div><h3><strong>Step 3: Write Code (Optimal)</strong></h3><h4><strong>Python</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!r3zS!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faad78dce-0c4c-4062-82c9-64556e8d0d10_1254x866.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!r3zS!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faad78dce-0c4c-4062-82c9-64556e8d0d10_1254x866.png 424w, /__u/substackcdn.com/image/fetch/$s_!r3zS!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faad78dce-0c4c-4062-82c9-64556e8d0d10_1254x866.png 848w, /__u/substackcdn.com/image/fetch/$s_!r3zS!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faad78dce-0c4c-4062-82c9-64556e8d0d10_1254x866.png 1272w, /__u/substackcdn.com/image/fetch/$s_!r3zS!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faad78dce-0c4c-4062-82c9-64556e8d0d10_1254x866.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!r3zS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faad78dce-0c4c-4062-82c9-64556e8d0d10_1254x866.png" width="1254" height="866" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/aad78dce-0c4c-4062-82c9-64556e8d0d10_1254x866.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:866,&quot;width&quot;:1254,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:112223,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/170044853?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faad78dce-0c4c-4062-82c9-64556e8d0d10_1254x866.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!r3zS!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faad78dce-0c4c-4062-82c9-64556e8d0d10_1254x866.png 424w, /__u/substackcdn.com/image/fetch/$s_!r3zS!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faad78dce-0c4c-4062-82c9-64556e8d0d10_1254x866.png 848w, /__u/substackcdn.com/image/fetch/$s_!r3zS!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faad78dce-0c4c-4062-82c9-64556e8d0d10_1254x866.png 1272w, /__u/substackcdn.com/image/fetch/$s_!r3zS!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faad78dce-0c4c-4062-82c9-64556e8d0d10_1254x866.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4><strong>Java</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!VMFM!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa61951ac-d8e7-4482-a750-57fd3e0cd5a8_1166x1266.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!VMFM!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa61951ac-d8e7-4482-a750-57fd3e0cd5a8_1166x1266.png 424w, /__u/substackcdn.com/image/fetch/$s_!VMFM!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa61951ac-d8e7-4482-a750-57fd3e0cd5a8_1166x1266.png 848w, /__u/substackcdn.com/image/fetch/$s_!VMFM!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa61951ac-d8e7-4482-a750-57fd3e0cd5a8_1166x1266.png 1272w, /__u/substackcdn.com/image/fetch/$s_!VMFM!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa61951ac-d8e7-4482-a750-57fd3e0cd5a8_1166x1266.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!VMFM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa61951ac-d8e7-4482-a750-57fd3e0cd5a8_1166x1266.png" width="1166" height="1266" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a61951ac-d8e7-4482-a750-57fd3e0cd5a8_1166x1266.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1266,&quot;width&quot;:1166,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:218772,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/170044853?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa61951ac-d8e7-4482-a750-57fd3e0cd5a8_1166x1266.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!VMFM!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa61951ac-d8e7-4482-a750-57fd3e0cd5a8_1166x1266.png 424w, /__u/substackcdn.com/image/fetch/$s_!VMFM!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa61951ac-d8e7-4482-a750-57fd3e0cd5a8_1166x1266.png 848w, /__u/substackcdn.com/image/fetch/$s_!VMFM!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa61951ac-d8e7-4482-a750-57fd3e0cd5a8_1166x1266.png 1272w, /__u/substackcdn.com/image/fetch/$s_!VMFM!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa61951ac-d8e7-4482-a750-57fd3e0cd5a8_1166x1266.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4><strong>C++</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!nE72!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8edea705-d452-44e0-bc13-fc9c1ae152bc_1200x1180.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!nE72!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8edea705-d452-44e0-bc13-fc9c1ae152bc_1200x1180.png 424w, /__u/substackcdn.com/image/fetch/$s_!nE72!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8edea705-d452-44e0-bc13-fc9c1ae152bc_1200x1180.png 848w, /__u/substackcdn.com/image/fetch/$s_!nE72!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8edea705-d452-44e0-bc13-fc9c1ae152bc_1200x1180.png 1272w, /__u/substackcdn.com/image/fetch/$s_!nE72!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8edea705-d452-44e0-bc13-fc9c1ae152bc_1200x1180.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!nE72!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8edea705-d452-44e0-bc13-fc9c1ae152bc_1200x1180.png" width="1200" height="1180" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8edea705-d452-44e0-bc13-fc9c1ae152bc_1200x1180.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1180,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:201504,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/170044853?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8edea705-d452-44e0-bc13-fc9c1ae152bc_1200x1180.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!nE72!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8edea705-d452-44e0-bc13-fc9c1ae152bc_1200x1180.png 424w, /__u/substackcdn.com/image/fetch/$s_!nE72!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8edea705-d452-44e0-bc13-fc9c1ae152bc_1200x1180.png 848w, /__u/substackcdn.com/image/fetch/$s_!nE72!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8edea705-d452-44e0-bc13-fc9c1ae152bc_1200x1180.png 1272w, /__u/substackcdn.com/image/fetch/$s_!nE72!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8edea705-d452-44e0-bc13-fc9c1ae152bc_1200x1180.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h3><strong>Step 4: Answer Follow-Ups</strong></h3><ul><li><p><strong>What if duplicates exist?</strong></p><ul><li><p>Set removes them automatically.</p></li></ul></li><li><p><strong>Can we return the sequence itself?</strong></p><ul><li><p>Yes, keep track of start.</p></li><li><p>Build the sequence after scanning.</p></li></ul></li></ul>]]></content:encoded></item><item><title><![CDATA[Valid Anagram]]></title><description><![CDATA[Meta Interview Question]]></description><link>https://singhz.substack.com/p/valid-anagram-283</link><guid isPermaLink="false">https://singhz.substack.com/p/valid-anagram-283</guid><dc:creator><![CDATA[Harpreet Singh]]></dc:creator><pubDate>Mon, 29 Jun 2026 14:00:55 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!L1hp!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F254cb6bf-6d52-4ebb-979d-ba17300e6c96_1280x720.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!L1hp!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F254cb6bf-6d52-4ebb-979d-ba17300e6c96_1280x720.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!L1hp!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F254cb6bf-6d52-4ebb-979d-ba17300e6c96_1280x720.png 424w, /__u/substackcdn.com/image/fetch/$s_!L1hp!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F254cb6bf-6d52-4ebb-979d-ba17300e6c96_1280x720.png 848w, /__u/substackcdn.com/image/fetch/$s_!L1hp!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F254cb6bf-6d52-4ebb-979d-ba17300e6c96_1280x720.png 1272w, /__u/substackcdn.com/image/fetch/$s_!L1hp!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F254cb6bf-6d52-4ebb-979d-ba17300e6c96_1280x720.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!L1hp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F254cb6bf-6d52-4ebb-979d-ba17300e6c96_1280x720.png" width="1280" height="720" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/254cb6bf-6d52-4ebb-979d-ba17300e6c96_1280x720.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:720,&quot;width&quot;:1280,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:305678,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/200948335?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F254cb6bf-6d52-4ebb-979d-ba17300e6c96_1280x720.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!L1hp!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F254cb6bf-6d52-4ebb-979d-ba17300e6c96_1280x720.png 424w, /__u/substackcdn.com/image/fetch/$s_!L1hp!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F254cb6bf-6d52-4ebb-979d-ba17300e6c96_1280x720.png 848w, /__u/substackcdn.com/image/fetch/$s_!L1hp!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F254cb6bf-6d52-4ebb-979d-ba17300e6c96_1280x720.png 1272w, /__u/substackcdn.com/image/fetch/$s_!L1hp!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F254cb6bf-6d52-4ebb-979d-ba17300e6c96_1280x720.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3><strong>Problem Overview</strong></h3><blockquote><p><strong>Difficulty: </strong>Easy<br><strong>LeetCode Pattern:</strong> Arrays &amp; Hashing</p></blockquote><p>Given two strings <strong>s</strong> and <strong>t</strong>, return <strong>true</strong> if <strong>t</strong> is an anagram of <strong>s</strong>, and <strong>false</strong> otherwise.</p><p><em>Note: Anagram is a word or phrase formed by rearranging the letters of another word or phrase.</em></p><pre><code><strong>Input:</strong> 
&#183; s = "anagram"
&#183; t = "nagaram"
<strong>Output:</strong> 
&#183; true
<strong>Explanation:</strong> 
&#183; Both strings contain:
  &#183; 'a': 3 times
  &#183; 'n': 1 time
  &#183; 'g': 1 time
  &#183; 'r': 1 time
  &#183; 'm': 1 time
&#183; So they are anagrams.

<strong>Input:</strong> 
&#183; s = "ram"
&#183; t = "car"
<strong>Output:</strong> 
&#183; false
<strong>Explanation:</strong>
&#183; String s contains 'm'.
&#183; But string t does not.
&#183; So they are not anagrams.</code></pre><div><hr></div><h3><strong>Step 1: Clarify Requirements</strong></h3><ul><li><p><strong>Can the input strings be empty?</strong></p><ul><li><p>Yes. Return true.</p></li></ul></li><li><p><strong>Can they contain uppercase characters?</strong></p><ul><li><p>Yes.</p></li></ul></li><li><p><strong>Are strings ASCII (128 characters)?</strong></p><ul><li><p>Yes.</p></li></ul></li></ul><div><hr></div><h3><strong>Step 2: Discuss Approaches</strong></h3><p><strong>1/ Sort Strings (Brute Force &#9888;&#65039;)</strong></p><ul><li><p><strong>Logic:</strong></p><ul><li><p>Sort both the strings</p></li><li><p>Return <strong>true</strong> if they are equal</p></li><li><p>Return <strong>false</strong> otherwise</p></li></ul></li><li><p><strong>Big O:</strong></p><ul><li><p>Time Complexity: O(n&#183;logn)</p></li><li><p>Space Complexity: O(n)</p></li></ul></li></ul><div><hr></div><p><strong>2/ Use a Map (Optimal &#9989;)</strong></p><ul><li><p><strong>Logic:</strong></p><ul><li><p>Initialize an empty map</p><ul><li><p>Key will be character</p></li><li><p>Value will be char frequency</p></li></ul></li><li><p>Loop through the first string:</p><ul><li><p>+1 to each char&#8217;s count</p></li></ul></li><li><p>Loop through the second string:</p><ul><li><p>-1 to each char&#8217;s count</p></li></ul></li><li><p>At the end, check if any count &#8800; 0</p><ul><li><p>Return false &#8594; not an anagram</p></li></ul></li><li><p>Otherwise, return true</p></li></ul></li><li><p><strong>Big O:</strong></p><ul><li><p>Time Complexity: O(n)</p></li><li><p>Space Complexity: O(1)</p><ul><li><p>Max 128 keys in map (constant)</p></li></ul></li></ul></li></ul><div><hr></div><h3><strong>Step 3: Write Code (Optimal)</strong></h3><h4><strong>Python</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Fwzj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33abc557-5e5c-44a4-9511-67159fdd3b09_846x876.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Fwzj!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33abc557-5e5c-44a4-9511-67159fdd3b09_846x876.png 424w, /__u/substackcdn.com/image/fetch/$s_!Fwzj!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33abc557-5e5c-44a4-9511-67159fdd3b09_846x876.png 848w, /__u/substackcdn.com/image/fetch/$s_!Fwzj!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33abc557-5e5c-44a4-9511-67159fdd3b09_846x876.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Fwzj!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33abc557-5e5c-44a4-9511-67159fdd3b09_846x876.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Fwzj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33abc557-5e5c-44a4-9511-67159fdd3b09_846x876.png" width="846" height="876" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/33abc557-5e5c-44a4-9511-67159fdd3b09_846x876.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:876,&quot;width&quot;:846,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:104450,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/200948335?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33abc557-5e5c-44a4-9511-67159fdd3b09_846x876.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!Fwzj!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33abc557-5e5c-44a4-9511-67159fdd3b09_846x876.png 424w, /__u/substackcdn.com/image/fetch/$s_!Fwzj!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33abc557-5e5c-44a4-9511-67159fdd3b09_846x876.png 848w, /__u/substackcdn.com/image/fetch/$s_!Fwzj!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33abc557-5e5c-44a4-9511-67159fdd3b09_846x876.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Fwzj!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33abc557-5e5c-44a4-9511-67159fdd3b09_846x876.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4><strong>Java</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Hmdz!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0dcd79a4-d134-49dc-8d96-22e28c2f33a4_948x978.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Hmdz!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0dcd79a4-d134-49dc-8d96-22e28c2f33a4_948x978.png 424w, /__u/substackcdn.com/image/fetch/$s_!Hmdz!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0dcd79a4-d134-49dc-8d96-22e28c2f33a4_948x978.png 848w, /__u/substackcdn.com/image/fetch/$s_!Hmdz!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0dcd79a4-d134-49dc-8d96-22e28c2f33a4_948x978.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Hmdz!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0dcd79a4-d134-49dc-8d96-22e28c2f33a4_948x978.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Hmdz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0dcd79a4-d134-49dc-8d96-22e28c2f33a4_948x978.png" width="948" height="978" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0dcd79a4-d134-49dc-8d96-22e28c2f33a4_948x978.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:978,&quot;width&quot;:948,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:125826,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/200948335?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0dcd79a4-d134-49dc-8d96-22e28c2f33a4_948x978.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!Hmdz!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0dcd79a4-d134-49dc-8d96-22e28c2f33a4_948x978.png 424w, /__u/substackcdn.com/image/fetch/$s_!Hmdz!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0dcd79a4-d134-49dc-8d96-22e28c2f33a4_948x978.png 848w, /__u/substackcdn.com/image/fetch/$s_!Hmdz!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0dcd79a4-d134-49dc-8d96-22e28c2f33a4_948x978.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Hmdz!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0dcd79a4-d134-49dc-8d96-22e28c2f33a4_948x978.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4><strong>C++</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!MXma!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf1ce1bb-b3d6-41ec-b892-0cc701fbae9c_848x970.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!MXma!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf1ce1bb-b3d6-41ec-b892-0cc701fbae9c_848x970.png 424w, /__u/substackcdn.com/image/fetch/$s_!MXma!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf1ce1bb-b3d6-41ec-b892-0cc701fbae9c_848x970.png 848w, /__u/substackcdn.com/image/fetch/$s_!MXma!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf1ce1bb-b3d6-41ec-b892-0cc701fbae9c_848x970.png 1272w, /__u/substackcdn.com/image/fetch/$s_!MXma!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf1ce1bb-b3d6-41ec-b892-0cc701fbae9c_848x970.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!MXma!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf1ce1bb-b3d6-41ec-b892-0cc701fbae9c_848x970.png" width="848" height="970" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/af1ce1bb-b3d6-41ec-b892-0cc701fbae9c_848x970.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:970,&quot;width&quot;:848,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:108274,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/200948335?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf1ce1bb-b3d6-41ec-b892-0cc701fbae9c_848x970.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!MXma!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf1ce1bb-b3d6-41ec-b892-0cc701fbae9c_848x970.png 424w, /__u/substackcdn.com/image/fetch/$s_!MXma!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf1ce1bb-b3d6-41ec-b892-0cc701fbae9c_848x970.png 848w, /__u/substackcdn.com/image/fetch/$s_!MXma!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf1ce1bb-b3d6-41ec-b892-0cc701fbae9c_848x970.png 1272w, /__u/substackcdn.com/image/fetch/$s_!MXma!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf1ce1bb-b3d6-41ec-b892-0cc701fbae9c_848x970.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h3><strong>Step 4: Answer Follow-Ups</strong></h3><ul><li><p><strong>What if you can&#8217;t use a map?</strong></p><ul><li><p>Use a fixed-size array for counts.</p></li></ul></li><li><p><strong>What if case doesn&#8217;t matter?</strong></p><ul><li><p>Convert both strings to lowercase.</p></li><li><p>Then run our same algorithm.</p></li></ul></li><li><p><strong>What if input contains Unicode?</strong></p><ul><li><p>Our logic still works.</p></li><li><p>But space complexity changes to O(n).</p></li></ul></li></ul>]]></content:encoded></item><item><title><![CDATA[Best Time to Buy and Sell Stock]]></title><description><![CDATA[Amazon Interview Question]]></description><link>https://singhz.substack.com/p/best-time-to-buy-and-sell-stock-e68</link><guid isPermaLink="false">https://singhz.substack.com/p/best-time-to-buy-and-sell-stock-e68</guid><dc:creator><![CDATA[Harpreet Singh]]></dc:creator><pubDate>Mon, 22 Jun 2026 14:00:38 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!IFyP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0408ea91-db0f-46b8-aabe-7d9a944b8fb1_1280x720.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!IFyP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0408ea91-db0f-46b8-aabe-7d9a944b8fb1_1280x720.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!IFyP!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0408ea91-db0f-46b8-aabe-7d9a944b8fb1_1280x720.png 424w, /__u/substackcdn.com/image/fetch/$s_!IFyP!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0408ea91-db0f-46b8-aabe-7d9a944b8fb1_1280x720.png 848w, /__u/substackcdn.com/image/fetch/$s_!IFyP!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0408ea91-db0f-46b8-aabe-7d9a944b8fb1_1280x720.png 1272w, /__u/substackcdn.com/image/fetch/$s_!IFyP!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0408ea91-db0f-46b8-aabe-7d9a944b8fb1_1280x720.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!IFyP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0408ea91-db0f-46b8-aabe-7d9a944b8fb1_1280x720.png" width="1280" height="720" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0408ea91-db0f-46b8-aabe-7d9a944b8fb1_1280x720.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:720,&quot;width&quot;:1280,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:312763,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/200947558?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0408ea91-db0f-46b8-aabe-7d9a944b8fb1_1280x720.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!IFyP!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0408ea91-db0f-46b8-aabe-7d9a944b8fb1_1280x720.png 424w, /__u/substackcdn.com/image/fetch/$s_!IFyP!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0408ea91-db0f-46b8-aabe-7d9a944b8fb1_1280x720.png 848w, /__u/substackcdn.com/image/fetch/$s_!IFyP!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0408ea91-db0f-46b8-aabe-7d9a944b8fb1_1280x720.png 1272w, /__u/substackcdn.com/image/fetch/$s_!IFyP!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0408ea91-db0f-46b8-aabe-7d9a944b8fb1_1280x720.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3><strong>Problem Overview</strong></h3><blockquote><p><strong>Difficulty: </strong>Easy<br><strong>LeetCode Pattern:</strong> Arrays &amp; Hashing</p></blockquote><p>You are given an array <strong>prices</strong> where <strong>prices[i]</strong> is the stock price on <strong>i<sup>th</sup> </strong>day.</p><p>Return the <strong>max profit</strong> you can make by buying on one day and selling on a later day.</p><p>Return <strong>0</strong> if no profit is possible.</p><pre><code><strong>Input:</strong> prices = [3,2,1]
<strong>Output:</strong> 0
<strong>Reason:</strong>
&#183; Prices are only decreasing.
&#183; So can't make any profit.

<strong>Input:</strong> prices = [2,1,5]
<strong>Output:</strong> 4
<strong>Reason:</strong> 
&#183; Buy at price = 1
&#183; Sell at price = 5
&#183; Profit = 5-1 = 4</code></pre><div><hr></div><h3><strong>Step 1: Clarify Requirements</strong></h3><ul><li><p><strong>Can the input array be empty?</strong></p><ul><li><p>Yes.</p></li><li><p>Return 0 in that case.</p></li></ul></li><li><p><strong>Will it be sorted?</strong></p><ul><li><p>No, not always.</p></li></ul></li><li><p><strong>Could prices be negative?</strong></p><ul><li><p>Assume they won&#8217;t be.</p></li></ul></li></ul><div><hr></div><h3><strong>Step 2: Discuss Approaches</strong></h3><p><strong>1/ Nested Loop (Brute Force &#9888;&#65039;)</strong></p><ul><li><p><strong>Logic:</strong></p><ul><li><p>For each price:</p><ul><li><p>Try selling it on all future days</p></li><li><p>Keep track of the max profit</p></li></ul></li><li><p>Return the max profit in the end</p></li></ul></li><li><p><strong>Big O:</strong></p><ul><li><p>Time Complexity: O(n&#178;)</p></li><li><p>Space Complexity: O(1)</p></li></ul></li></ul><div><hr></div><p><strong>2/ Track Min Price (Optimal &#9989;)</strong></p><ul><li><p><strong>Logic:</strong></p><ul><li><p>Keep track of min price at all times</p></li><li><p>For each price:</p><ul><li><p>Update min price if needed</p></li><li><p>Update max profit if needed</p></li></ul></li><li><p>Return the max profit in the end</p></li></ul></li><li><p><strong>Big O:</strong></p><ul><li><p>Time Complexity: O(n)</p></li><li><p>Space Complexity: O(1)</p></li></ul></li></ul><div><hr></div><h3><strong>Step 3: Write Code (Optimal)</strong></h3><h4><strong>Python</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!P3mY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28d4fd12-6353-4450-a299-9443a9c36d4c_1042x720.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!P3mY!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28d4fd12-6353-4450-a299-9443a9c36d4c_1042x720.png 424w, /__u/substackcdn.com/image/fetch/$s_!P3mY!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28d4fd12-6353-4450-a299-9443a9c36d4c_1042x720.png 848w, /__u/substackcdn.com/image/fetch/$s_!P3mY!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28d4fd12-6353-4450-a299-9443a9c36d4c_1042x720.png 1272w, /__u/substackcdn.com/image/fetch/$s_!P3mY!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28d4fd12-6353-4450-a299-9443a9c36d4c_1042x720.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!P3mY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28d4fd12-6353-4450-a299-9443a9c36d4c_1042x720.png" width="1042" height="720" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/28d4fd12-6353-4450-a299-9443a9c36d4c_1042x720.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:720,&quot;width&quot;:1042,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:106991,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/200947558?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28d4fd12-6353-4450-a299-9443a9c36d4c_1042x720.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!P3mY!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28d4fd12-6353-4450-a299-9443a9c36d4c_1042x720.png 424w, /__u/substackcdn.com/image/fetch/$s_!P3mY!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28d4fd12-6353-4450-a299-9443a9c36d4c_1042x720.png 848w, /__u/substackcdn.com/image/fetch/$s_!P3mY!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28d4fd12-6353-4450-a299-9443a9c36d4c_1042x720.png 1272w, /__u/substackcdn.com/image/fetch/$s_!P3mY!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28d4fd12-6353-4450-a299-9443a9c36d4c_1042x720.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4><strong>Java</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Pdbr!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6bb6234-6d7c-4d93-b6b9-8854c05f9240_1136x838.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Pdbr!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6bb6234-6d7c-4d93-b6b9-8854c05f9240_1136x838.png 424w, /__u/substackcdn.com/image/fetch/$s_!Pdbr!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6bb6234-6d7c-4d93-b6b9-8854c05f9240_1136x838.png 848w, /__u/substackcdn.com/image/fetch/$s_!Pdbr!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6bb6234-6d7c-4d93-b6b9-8854c05f9240_1136x838.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Pdbr!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6bb6234-6d7c-4d93-b6b9-8854c05f9240_1136x838.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Pdbr!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6bb6234-6d7c-4d93-b6b9-8854c05f9240_1136x838.png" width="1136" height="838" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e6bb6234-6d7c-4d93-b6b9-8854c05f9240_1136x838.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:838,&quot;width&quot;:1136,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:121374,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/200947558?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6bb6234-6d7c-4d93-b6b9-8854c05f9240_1136x838.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!Pdbr!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6bb6234-6d7c-4d93-b6b9-8854c05f9240_1136x838.png 424w, /__u/substackcdn.com/image/fetch/$s_!Pdbr!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6bb6234-6d7c-4d93-b6b9-8854c05f9240_1136x838.png 848w, /__u/substackcdn.com/image/fetch/$s_!Pdbr!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6bb6234-6d7c-4d93-b6b9-8854c05f9240_1136x838.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Pdbr!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6bb6234-6d7c-4d93-b6b9-8854c05f9240_1136x838.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4><strong>C++</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!nRCA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf55d046-b53f-40ec-8cb6-c366c3899fb0_1064x818.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!nRCA!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf55d046-b53f-40ec-8cb6-c366c3899fb0_1064x818.png 424w, /__u/substackcdn.com/image/fetch/$s_!nRCA!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf55d046-b53f-40ec-8cb6-c366c3899fb0_1064x818.png 848w, /__u/substackcdn.com/image/fetch/$s_!nRCA!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf55d046-b53f-40ec-8cb6-c366c3899fb0_1064x818.png 1272w, /__u/substackcdn.com/image/fetch/$s_!nRCA!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf55d046-b53f-40ec-8cb6-c366c3899fb0_1064x818.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!nRCA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf55d046-b53f-40ec-8cb6-c366c3899fb0_1064x818.png" width="1064" height="818" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/df55d046-b53f-40ec-8cb6-c366c3899fb0_1064x818.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:818,&quot;width&quot;:1064,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:114400,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/200947558?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf55d046-b53f-40ec-8cb6-c366c3899fb0_1064x818.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!nRCA!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf55d046-b53f-40ec-8cb6-c366c3899fb0_1064x818.png 424w, /__u/substackcdn.com/image/fetch/$s_!nRCA!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf55d046-b53f-40ec-8cb6-c366c3899fb0_1064x818.png 848w, /__u/substackcdn.com/image/fetch/$s_!nRCA!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf55d046-b53f-40ec-8cb6-c366c3899fb0_1064x818.png 1272w, /__u/substackcdn.com/image/fetch/$s_!nRCA!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf55d046-b53f-40ec-8cb6-c366c3899fb0_1064x818.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h3><strong>Step 4: Answer Follow-Ups</strong></h3><ul><li><p><strong>How to return the days (not prices)?</strong></p><ul><li><p>Track the min price day index</p></li><li><p>When updating max profit:</p><ul><li><p>Store the min price day index</p></li><li><p>Store the current day index</p></li></ul></li><li><p>Return both indices at the end</p></li></ul></li><li><p><strong>What if multiple buys and sells were allowed?</strong></p><ul><li><p>No need to track min price</p></li><li><p>Keep track of total profit</p></li><li><p>Anytime the price increases:</p><ul><li><p>add the difference to total profit</p></li></ul></li><li><p>Return this total profit at the end</p></li></ul></li></ul>]]></content:encoded></item><item><title><![CDATA[Two Sum]]></title><description><![CDATA[Google Interview Question]]></description><link>https://singhz.substack.com/p/two-sum-283</link><guid isPermaLink="false">https://singhz.substack.com/p/two-sum-283</guid><dc:creator><![CDATA[Harpreet Singh]]></dc:creator><pubDate>Mon, 15 Jun 2026 14:01:09 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!CSx2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca57975d-e92f-45ee-93ee-6739c0cb9d59_1280x720.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!CSx2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca57975d-e92f-45ee-93ee-6739c0cb9d59_1280x720.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!CSx2!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca57975d-e92f-45ee-93ee-6739c0cb9d59_1280x720.png 424w, /__u/substackcdn.com/image/fetch/$s_!CSx2!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca57975d-e92f-45ee-93ee-6739c0cb9d59_1280x720.png 848w, /__u/substackcdn.com/image/fetch/$s_!CSx2!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca57975d-e92f-45ee-93ee-6739c0cb9d59_1280x720.png 1272w, /__u/substackcdn.com/image/fetch/$s_!CSx2!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca57975d-e92f-45ee-93ee-6739c0cb9d59_1280x720.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!CSx2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca57975d-e92f-45ee-93ee-6739c0cb9d59_1280x720.png" width="1280" height="720" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ca57975d-e92f-45ee-93ee-6739c0cb9d59_1280x720.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:720,&quot;width&quot;:1280,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:297175,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/200945579?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca57975d-e92f-45ee-93ee-6739c0cb9d59_1280x720.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!CSx2!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca57975d-e92f-45ee-93ee-6739c0cb9d59_1280x720.png 424w, /__u/substackcdn.com/image/fetch/$s_!CSx2!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca57975d-e92f-45ee-93ee-6739c0cb9d59_1280x720.png 848w, /__u/substackcdn.com/image/fetch/$s_!CSx2!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca57975d-e92f-45ee-93ee-6739c0cb9d59_1280x720.png 1272w, /__u/substackcdn.com/image/fetch/$s_!CSx2!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca57975d-e92f-45ee-93ee-6739c0cb9d59_1280x720.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3><strong>Problem Overview</strong></h3><blockquote><p><strong>Difficulty: </strong>Easy<br><strong>LeetCode Pattern:</strong> Arrays &amp; Hashing</p></blockquote><p>Given an array of integers <strong>nums</strong> and an integer <strong>target</strong>, return indices of the two numbers such that they add up to <strong>target</strong>.</p><pre><code><strong>Input:</strong> 
&#183; nums = [3,3]
&#183; target = 6
<strong>Output:</strong> 
&#183; [0,1]

<strong>Input:</strong> 
&#183; nums = [2,7,11,15]
&#183; target = 18
<strong>Output:</strong> 
&#183; [1,2]</code></pre><div><hr></div><h3><strong>Step 1: Clarify Requirements</strong></h3><ul><li><p><strong>Can the input array be empty?</strong></p><ul><li><p>Yes. </p></li><li><p>Return empty array.</p></li></ul></li><li><p><strong>Will it be sorted?</strong></p><ul><li><p>No.</p></li></ul></li></ul><div><hr></div><h3><strong>Step 2: Discuss Approaches</strong></h3><p><strong>1/ Nested Loop (Brute Force &#9888;&#65039;)</strong></p><ul><li><p><strong>Logic:</strong></p><ul><li><p>Start with value at index 0</p></li><li><p>Check its sum with all other values</p></li><li><p>If it adds up to target:</p><ul><li><p>return the indices</p></li></ul></li><li><p>Otherwise:</p><ul><li><p>increment index and repeat</p></li></ul></li></ul></li><li><p><strong>Big O:</strong></p><ul><li><p>Time Complexity: O(n&#178;)</p></li><li><p>Space Complexity: O(1)</p></li></ul></li></ul><div><hr></div><p><strong>2/ Use a Map (Optimal &#9989;)</strong></p><ul><li><p><strong>Logic:</strong></p><ul><li><p>Initialize an empty map</p></li><li><p>Iterate through the list</p></li><li><p>For each element:</p><ul><li><p>Check if complement is in map</p></li><li><p>If it exists:</p><ul><li><p>We found a solution</p></li></ul></li><li><p>Otherwise:</p><ul><li><p>Store in map and continue</p></li></ul></li></ul></li><li><p>Return empty array if no pair found</p></li></ul></li><li><p><strong>Big O:</strong></p><ul><li><p>Time Complexity: O(n)</p></li><li><p>Space Complexity: O(n)</p></li></ul></li></ul><div><hr></div><h3><strong>Step 3: Write Code (Optimal)</strong></h3><h4><strong>Python</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!CMcH!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d2ab85a-c0ca-439f-a6be-94175b09c05b_1080x898.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!CMcH!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d2ab85a-c0ca-439f-a6be-94175b09c05b_1080x898.png 424w, /__u/substackcdn.com/image/fetch/$s_!CMcH!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d2ab85a-c0ca-439f-a6be-94175b09c05b_1080x898.png 848w, /__u/substackcdn.com/image/fetch/$s_!CMcH!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d2ab85a-c0ca-439f-a6be-94175b09c05b_1080x898.png 1272w, /__u/substackcdn.com/image/fetch/$s_!CMcH!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d2ab85a-c0ca-439f-a6be-94175b09c05b_1080x898.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!CMcH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d2ab85a-c0ca-439f-a6be-94175b09c05b_1080x898.png" width="1080" height="898" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2d2ab85a-c0ca-439f-a6be-94175b09c05b_1080x898.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:898,&quot;width&quot;:1080,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:139057,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/200945579?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d2ab85a-c0ca-439f-a6be-94175b09c05b_1080x898.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!CMcH!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d2ab85a-c0ca-439f-a6be-94175b09c05b_1080x898.png 424w, /__u/substackcdn.com/image/fetch/$s_!CMcH!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d2ab85a-c0ca-439f-a6be-94175b09c05b_1080x898.png 848w, /__u/substackcdn.com/image/fetch/$s_!CMcH!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d2ab85a-c0ca-439f-a6be-94175b09c05b_1080x898.png 1272w, /__u/substackcdn.com/image/fetch/$s_!CMcH!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d2ab85a-c0ca-439f-a6be-94175b09c05b_1080x898.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4><strong>Java</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!HUHs!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd72510e-968f-49d1-aac5-77bcc984ee89_1232x1006.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!HUHs!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd72510e-968f-49d1-aac5-77bcc984ee89_1232x1006.png 424w, /__u/substackcdn.com/image/fetch/$s_!HUHs!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd72510e-968f-49d1-aac5-77bcc984ee89_1232x1006.png 848w, /__u/substackcdn.com/image/fetch/$s_!HUHs!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd72510e-968f-49d1-aac5-77bcc984ee89_1232x1006.png 1272w, /__u/substackcdn.com/image/fetch/$s_!HUHs!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd72510e-968f-49d1-aac5-77bcc984ee89_1232x1006.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!HUHs!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd72510e-968f-49d1-aac5-77bcc984ee89_1232x1006.png" width="1232" height="1006" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/dd72510e-968f-49d1-aac5-77bcc984ee89_1232x1006.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1006,&quot;width&quot;:1232,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:180821,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/200945579?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd72510e-968f-49d1-aac5-77bcc984ee89_1232x1006.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!HUHs!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd72510e-968f-49d1-aac5-77bcc984ee89_1232x1006.png 424w, /__u/substackcdn.com/image/fetch/$s_!HUHs!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd72510e-968f-49d1-aac5-77bcc984ee89_1232x1006.png 848w, /__u/substackcdn.com/image/fetch/$s_!HUHs!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd72510e-968f-49d1-aac5-77bcc984ee89_1232x1006.png 1272w, /__u/substackcdn.com/image/fetch/$s_!HUHs!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd72510e-968f-49d1-aac5-77bcc984ee89_1232x1006.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4><strong>C++</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!i2Ml!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b94c1fd-b4c7-4081-b16a-71d81e4c248b_1044x1008.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!i2Ml!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b94c1fd-b4c7-4081-b16a-71d81e4c248b_1044x1008.png 424w, /__u/substackcdn.com/image/fetch/$s_!i2Ml!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b94c1fd-b4c7-4081-b16a-71d81e4c248b_1044x1008.png 848w, /__u/substackcdn.com/image/fetch/$s_!i2Ml!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b94c1fd-b4c7-4081-b16a-71d81e4c248b_1044x1008.png 1272w, /__u/substackcdn.com/image/fetch/$s_!i2Ml!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b94c1fd-b4c7-4081-b16a-71d81e4c248b_1044x1008.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!i2Ml!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b94c1fd-b4c7-4081-b16a-71d81e4c248b_1044x1008.png" width="1044" height="1008" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9b94c1fd-b4c7-4081-b16a-71d81e4c248b_1044x1008.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1008,&quot;width&quot;:1044,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:159383,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/200945579?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b94c1fd-b4c7-4081-b16a-71d81e4c248b_1044x1008.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!i2Ml!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b94c1fd-b4c7-4081-b16a-71d81e4c248b_1044x1008.png 424w, /__u/substackcdn.com/image/fetch/$s_!i2Ml!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b94c1fd-b4c7-4081-b16a-71d81e4c248b_1044x1008.png 848w, /__u/substackcdn.com/image/fetch/$s_!i2Ml!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b94c1fd-b4c7-4081-b16a-71d81e4c248b_1044x1008.png 1272w, /__u/substackcdn.com/image/fetch/$s_!i2Ml!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b94c1fd-b4c7-4081-b16a-71d81e4c248b_1044x1008.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h3><strong>Step 4: Answer Follow-Ups</strong></h3><ul><li><p><strong>What if the array is sorted?</strong></p><ul><li><p>Use two-pointer technique</p></li><li><p>No map is needed</p></li></ul></li><li><p><strong>How to return all valid pairs?</strong></p><ul><li><p>Store pairs in a list instead of returning immediately</p></li><li><p>Return the list after processing all elements</p></li></ul></li></ul>]]></content:encoded></item><item><title><![CDATA[76 LeetCode Problems To Crack Apple]]></title><description><![CDATA[Apple Interview Roadmap]]></description><link>https://singhz.substack.com/p/xx-leetcode-problems-to-crack-apple</link><guid isPermaLink="false">https://singhz.substack.com/p/xx-leetcode-problems-to-crack-apple</guid><dc:creator><![CDATA[Harpreet Singh]]></dc:creator><pubDate>Mon, 08 Jun 2026 14:01:13 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!L5MO!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b91bc9d-e035-412a-a02f-0fab81e392eb_1536x1024.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!L5MO!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b91bc9d-e035-412a-a02f-0fab81e392eb_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!L5MO!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b91bc9d-e035-412a-a02f-0fab81e392eb_1536x1024.png 424w, /__u/substackcdn.com/image/fetch/$s_!L5MO!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b91bc9d-e035-412a-a02f-0fab81e392eb_1536x1024.png 848w, /__u/substackcdn.com/image/fetch/$s_!L5MO!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b91bc9d-e035-412a-a02f-0fab81e392eb_1536x1024.png 1272w, /__u/substackcdn.com/image/fetch/$s_!L5MO!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b91bc9d-e035-412a-a02f-0fab81e392eb_1536x1024.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!L5MO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b91bc9d-e035-412a-a02f-0fab81e392eb_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2b91bc9d-e035-412a-a02f-0fab81e392eb_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1912422,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/184592324?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b91bc9d-e035-412a-a02f-0fab81e392eb_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!L5MO!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b91bc9d-e035-412a-a02f-0fab81e392eb_1536x1024.png 424w, /__u/substackcdn.com/image/fetch/$s_!L5MO!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b91bc9d-e035-412a-a02f-0fab81e392eb_1536x1024.png 848w, /__u/substackcdn.com/image/fetch/$s_!L5MO!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b91bc9d-e035-412a-a02f-0fab81e392eb_1536x1024.png 1272w, /__u/substackcdn.com/image/fetch/$s_!L5MO!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b91bc9d-e035-412a-a02f-0fab81e392eb_1536x1024.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Here are <strong>76 LeetCode Problems</strong> I will solve to crack my <strong>Apple</strong> interview.</p><p>And no, I won&#8217;t &#8220;grind 500 problems.&#8221;</p><p>I will focus on the right ones.</p><p>These are the <strong>exact problems</strong> I will practice right before my <strong>Apple</strong> interview:</p><div><hr></div><h3><strong>1/ Arrays &amp; Hashing</strong></h3><ol><li><p><a href="https://leetcode.com/problems/two-sum/">Two Sum</a></p></li><li><p><a href="https://leetcode.com/problems/pascals-triangle/">Pascal&#8217;s Triangle</a></p></li><li><p><a href="https://leetcode.com/problems/valid-anagram/">Valid Anagram</a></p></li><li><p><a href="https://leetcode.com/problems/group-anagrams/">Group Anagrams</a></p></li><li><p><a href="https://leetcode.com/problems/valid-sudoku/">Valid Sudoku</a></p></li><li><p><a href="https://leetcode.com/problems/spiral-matrix/">Spiral Matrix</a></p></li></ol><h3><strong>2/ Two Pointers</strong></h3><ol><li><p><a href="https://leetcode.com/problems/merge-sorted-array/">Merge Sorted Array</a></p></li><li><p><a href="https://leetcode.com/problems/valid-palindrome/">Valid Palindrome</a></p></li><li><p><a href="https://leetcode.com/problems/rotate-array/">Rotate Array</a></p></li><li><p><a href="https://leetcode.com/problems/reverse-words-in-a-string/">Reverse Words in a String</a></p></li><li><p><a href="https://leetcode.com/problems/string-compression/">String Compression</a></p></li><li><p><a href="https://leetcode.com/problems/trapping-rain-water/">Trapping Rain Water</a></p></li></ol><h3><strong>3/ Stack</strong></h3><ol><li><p><a href="https://leetcode.com/problems/valid-parentheses/">Valid Parentheses</a></p></li><li><p><a href="https://leetcode.com/problems/implement-queue-using-stacks/">Implement Queue using Stacks</a></p></li><li><p><a href="http://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/">Remove All Adjacent Duplicates In String</a></p></li><li><p><a href="https://leetcode.com/problems/min-stack/">Min Stack</a></p></li><li><p><a href="https://leetcode.com/problems/decode-string/">Decode String</a></p></li><li><p><a href="https://leetcode.com/problems/flatten-nested-list-iterator/">Flatten Nested List Iterator</a></p></li></ol><h3><strong>4/ Binary Search</strong></h3><ol><li><p><a href="https://leetcode.com/problems/sqrtx/">Sqrt(x)</a></p></li><li><p><a href="https://leetcode.com/problems/time-based-key-value-store/">Time Based Key-Value Store</a></p></li><li><p><a href="https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/">Find First and Last Position of Element in Sorted Array</a></p></li><li><p><a href="https://leetcode.com/problems/search-in-rotated-sorted-array/">Search in Rotated Sorted Array</a></p></li><li><p><a href="https://leetcode.com/problems/snapshot-array/">Snapshot Array</a></p></li><li><p><a href="https://leetcode.com/problems/median-of-two-sorted-arrays/">Median of Two Sorted Arrays</a></p></li></ol><h3><strong>5/ Sliding Window</strong></h3><ol><li><p><a href="https://leetcode.com/problems/contains-duplicate-ii/">Contains Duplicate II</a></p></li><li><p><a href="https://leetcode.com/problems/longest-substring-without-repeating-characters/">Longest Substring Without Repeating Characters</a></p></li><li><p><a href="https://leetcode.com/problems/subarray-product-less-than-k/">Subarray Product Less Than K</a></p></li><li><p><a href="https://leetcode.com/problems/find-all-anagrams-in-a-string/">Find All Anagrams in a String</a></p></li><li><p><a href="https://leetcode.com/problems/sliding-window-maximum/">Sliding Window Maximum</a></p></li></ol><h3><strong>6/ Linked List</strong></h3><ol><li><p><a href="https://leetcode.com/problems/reverse-linked-list/">Reverse Linked List</a></p></li><li><p><a href="https://leetcode.com/problems/merge-two-sorted-lists/">Merge Two Sorted Lists</a></p></li><li><p><a href="https://leetcode.com/problems/lru-cache/">LRU Cache</a></p></li><li><p><a href="https://leetcode.com/problems/add-two-numbers/">Add Two Numbers</a></p></li><li><p><a href="https://leetcode.com/problems/delete-node-in-a-linked-list/">Delete Node in a Linked List</a></p></li><li><p><a href="https://leetcode.com/problems/remove-nth-node-from-end-of-list/">Remove Nth Node From End of List</a></p></li></ol><h3><strong>7/ Trees</strong></h3><ol><li><p><a href="https://leetcode.com/problems/maximum-depth-of-binary-tree/">Maximum Depth of Binary Tree</a></p></li><li><p><a href="https://leetcode.com/problems/binary-tree-paths/">Binary Tree Paths</a></p></li><li><p><a href="https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/">Lowest Common Ancestor of a Binary Tree</a></p></li><li><p><a href="https://leetcode.com/problems/binary-tree-level-order-traversal/">Binary Tree Level Order Traversal</a></p></li><li><p><a href="https://leetcode.com/problems/validate-binary-search-tree/">Validate Binary Search Tree</a></p></li><li><p><a href="https://leetcode.com/problems/serialize-and-deserialize-binary-tree/">Serialize and Deserialize Binary Tree</a></p></li></ol><h3><strong>8/ Trie</strong></h3><ol><li><p><a href="https://leetcode.com/problems/longest-common-prefix/">Longest Common Prefix</a></p></li><li><p><a href="https://leetcode.com/problems/word-break/">Word Break</a></p></li><li><p><a href="https://leetcode.com/problems/implement-trie-prefix-tree/">Implement Trie (Prefix Tree)</a></p></li><li><p><a href="https://leetcode.com/problems/design-add-and-search-words-data-structure/">Design Add And Search Words Data Structure</a></p></li></ol><h3><strong>9/ Backtracking</strong></h3><ol><li><p><a href="http://leetcode.com/problems/generate-parentheses/">Generate Parentheses</a></p></li><li><p><a href="https://leetcode.com/problems/maximum-rows-covered-by-columns/">Maximum Rows Covered by Columns</a></p></li><li><p><a href="https://leetcode.com/problems/permutations/">Permutations</a></p></li><li><p><a href="https://leetcode.com/problems/letter-combinations-of-a-phone-number/">Letter Combinations of a Phone Number</a></p></li><li><p><a href="https://leetcode.com/problems/word-search/">Word Search</a></p></li><li><p><a href="https://leetcode.com/problems/number-of-squareful-arrays/">Number of Squareful Arrays</a></p></li></ol><h3><strong>10/ Heap (Priority Queue)</strong></h3><ol><li><p><a href="https://leetcode.com/problems/top-k-frequent-elements/">Top K Frequent Elements</a></p></li><li><p><a href="https://leetcode.com/problems/kth-largest-element-in-an-array/">Kth Largest Element in an Array</a></p></li><li><p><a href="https://leetcode.com/problems/task-scheduler/">Task Scheduler</a></p></li><li><p><a href="https://leetcode.com/problems/merge-k-sorted-lists/">Merge k Sorted Lists</a></p></li><li><p><a href="https://leetcode.com/problems/find-median-from-data-stream/">Find Median from Data Stream</a></p></li><li><p><a href="https://leetcode.com/problems/find-servers-that-handled-most-number-of-requests/">Find Servers That Handled Most Number of Requests</a></p></li></ol><h3><strong>11/ Graphs</strong></h3><ol><li><p><a href="https://leetcode.com/problems/number-of-islands/">Number of Islands</a></p></li><li><p><a href="https://leetcode.com/problems/course-schedule/">Course Schedule</a></p></li><li><p><a href="https://leetcode.com/problems/course-schedule-ii/">Course Schedule II</a></p></li><li><p><a href="https://leetcode.com/problems/array-nesting/">Array Nesting</a></p></li><li><p><a href="http://leetcode.com/problems/word-ladder/">Word Ladder</a></p></li></ol><h3><strong>12/ Intervals</strong></h3><ol><li><p><a href="https://leetcode.com/problems/insert-interval/">Insert Interval</a></p></li><li><p><a href="https://leetcode.com/problems/interval-list-intersections/">Interval List Intersections</a></p></li><li><p><a href="https://leetcode.com/problems/merge-intervals/">Merge Intervals</a></p></li></ol><h3><strong>13/ Greedy &amp; Dynamic Programming</strong></h3><ol><li><p><a href="https://leetcode.com/problems/best-time-to-buy-and-sell-stock/">Best Time to Buy and Sell Stock</a></p></li><li><p><a href="https://leetcode.com/problems/maximum-subarray/">Maximum Subarray</a></p></li><li><p><a href="https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/">Best Time to Buy and Sell Stock II</a></p></li></ol><h3><strong>14/ Prefix Sum</strong></h3><ol><li><p><a href="https://leetcode.com/problems/product-of-array-except-self/">Product of Array Except Self</a></p></li><li><p><a href="https://leetcode.com/problems/subarray-sum-equals-k/">Subarray Sum Equals K</a></p></li><li><p><a href="https://leetcode.com/problems/random-pick-with-weight/">Random Pick with Weight</a></p></li></ol><h3><strong>15/ Miscellaneous</strong></h3><ol><li><p><a href="https://leetcode.com/problems/reverse-bits/">Reverse Bits</a></p></li><li><p><a href="https://leetcode.com/problems/climbing-stairs/">Climbing Stairs</a></p></li><li><p><a href="https://leetcode.com/problems/number-of-1-bits/">Number of 1 Bits</a></p></li><li><p><a href="https://leetcode.com/problems/reverse-integer/">Reverse Integer</a></p></li><li><p><a href="https://leetcode.com/problems/integer-to-english-words/">Integer to English Words</a></p></li></ol><div><hr></div><p>If you&#8217;re preparing for FAANG in 2026, don&#8217;t forget to save this post.</p><p>Also, here&#8217;s a platform which you can use to practice for your interviews:</p><p><strong>&#128279; Interview Roadmap Platform:</strong> <a href="https://interview-roadmap.vercel.app/">Link</a></p><p>Best of luck!</p>]]></content:encoded></item><item><title><![CDATA[77 LeetCode Problems To Crack TikTok]]></title><description><![CDATA[TikTok Interview Roadmap]]></description><link>https://singhz.substack.com/p/xx-leetcode-problems-to-crack-tiktok</link><guid isPermaLink="false">https://singhz.substack.com/p/xx-leetcode-problems-to-crack-tiktok</guid><dc:creator><![CDATA[Harpreet Singh]]></dc:creator><pubDate>Mon, 01 Jun 2026 14:01:22 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!ekOG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1ee7701-6c80-48d2-a997-1ef0acd1778f_1536x1024.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!ekOG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1ee7701-6c80-48d2-a997-1ef0acd1778f_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!ekOG!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1ee7701-6c80-48d2-a997-1ef0acd1778f_1536x1024.png 424w, /__u/substackcdn.com/image/fetch/$s_!ekOG!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1ee7701-6c80-48d2-a997-1ef0acd1778f_1536x1024.png 848w, /__u/substackcdn.com/image/fetch/$s_!ekOG!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1ee7701-6c80-48d2-a997-1ef0acd1778f_1536x1024.png 1272w, /__u/substackcdn.com/image/fetch/$s_!ekOG!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1ee7701-6c80-48d2-a997-1ef0acd1778f_1536x1024.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!ekOG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1ee7701-6c80-48d2-a997-1ef0acd1778f_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e1ee7701-6c80-48d2-a997-1ef0acd1778f_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1897244,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/184592181?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1ee7701-6c80-48d2-a997-1ef0acd1778f_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!ekOG!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1ee7701-6c80-48d2-a997-1ef0acd1778f_1536x1024.png 424w, /__u/substackcdn.com/image/fetch/$s_!ekOG!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1ee7701-6c80-48d2-a997-1ef0acd1778f_1536x1024.png 848w, /__u/substackcdn.com/image/fetch/$s_!ekOG!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1ee7701-6c80-48d2-a997-1ef0acd1778f_1536x1024.png 1272w, /__u/substackcdn.com/image/fetch/$s_!ekOG!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1ee7701-6c80-48d2-a997-1ef0acd1778f_1536x1024.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Here are <strong>77 LeetCode Problems</strong> I will solve to crack my <strong>TikTok</strong> interview.</p><p>And no, I won&#8217;t &#8220;grind 500 problems.&#8221;</p><p>I will focus on the right ones.</p><p>These are the <strong>exact problems</strong> I will practice right before my <strong>TikTok</strong> interview:</p><div><hr></div><h3><strong>1/ Arrays &amp; Hashing</strong></h3><ol><li><p><a href="https://leetcode.com/problems/two-sum/">Two Sum</a></p></li><li><p><a href="https://leetcode.com/problems/design-hashmap/">Design HashMap</a></p></li><li><p><a href="https://leetcode.com/problems/insert-delete-getrandom-o1/">Insert Delete GetRandom O(1)</a></p></li><li><p><a href="https://leetcode.com/problems/spiral-matrix/">Spiral Matrix</a></p></li><li><p><a href="https://leetcode.com/problems/longest-consecutive-sequence/">Longest Consecutive Sequence</a></p></li></ol><h3><strong>2/ Two Pointers</strong></h3><ol><li><p><a href="https://leetcode.com/problems/longest-palindromic-substring/">Longest Palindromic Substring</a></p></li><li><p><a href="https://leetcode.com/problems/3sum/">3Sum</a></p></li><li><p><a href="https://leetcode.com/problems/sort-colors/">Sort Colors</a></p></li><li><p><a href="https://leetcode.com/problems/container-with-most-water/">Container With Most Water</a></p></li><li><p><a href="https://leetcode.com/problems/compare-version-numbers/">Compare Version Numbers</a></p></li><li><p><a href="https://leetcode.com/problems/trapping-rain-water/">Trapping Rain Water</a></p></li></ol><h3><strong>3/ Stack</strong></h3><ol><li><p><a href="https://leetcode.com/problems/valid-parentheses/">Valid Parentheses</a></p></li><li><p><a href="https://leetcode.com/problems/decode-string/">Decode String</a></p></li><li><p><a href="https://leetcode.com/problems/basic-calculator-ii/">Basic Calculator II</a></p></li><li><p><a href="https://leetcode.com/problems/daily-temperatures/">Daily Temperatures</a></p></li><li><p><a href="https://leetcode.com/problems/asteroid-collision/">Asteroid Collision</a></p></li><li><p><a href="https://leetcode.com/problems/subarray-with-elements-greater-than-varying-threshold/">Subarray With Elements Greater Than Varying Threshold</a></p></li></ol><h3><strong>4/ Binary Search</strong></h3><ol><li><p><a href="https://leetcode.com/problems/sqrtx/">Sqrt(x)</a></p></li><li><p><a href="https://leetcode.com/problems/longest-increasing-subsequence/">Longest Increasing Subsequence</a></p></li><li><p><a href="https://leetcode.com/problems/search-in-rotated-sorted-array/">Search in Rotated Sorted Array</a></p></li><li><p><a href="https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/">Capacity To Ship Packages Within D Days</a></p></li><li><p><a href="https://leetcode.com/problems/maximum-value-at-a-given-index-in-a-bounded-array/">Maximum Value at a Given Index in a Bounded Array</a></p></li><li><p><a href="https://leetcode.com/problems/snapshot-array/">Snapshot Array</a></p></li><li><p><a href="https://leetcode.com/problems/find-peak-element/">Find Peak Element</a></p></li></ol><h3><strong>5/ Sliding Window</strong></h3><ol><li><p><a href="https://leetcode.com/problems/longest-substring-without-repeating-characters/">Longest Substring Without Repeating Characters</a></p></li><li><p><a href="https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/">Longest Substring with At Least K Repeating Characters</a></p></li><li><p><a href="https://leetcode.com/problems/count-complete-subarrays-in-an-array/">Count Complete Subarrays in an Array</a></p></li><li><p><a href="https://leetcode.com/problems/minimum-window-substring/">Minimum Window Substring</a></p></li><li><p><a href="https://leetcode.com/problems/minimum-moves-to-pick-k-ones/">Minimum Moves to Pick K Ones</a></p></li><li><p><a href="https://leetcode.com/problems/sliding-window-maximum/">Sliding Window Maximum</a></p></li></ol><h3><strong>6/ Linked List</strong></h3><ol><li><p><a href="https://leetcode.com/problems/reverse-linked-list/">Reverse Linked List</a></p></li><li><p><a href="https://leetcode.com/problems/merge-two-sorted-lists/">Merge Two Sorted Lists</a></p></li><li><p><a href="https://leetcode.com/problems/lru-cache/">LRU Cache</a></p></li><li><p><a href="https://leetcode.com/problems/reorder-list/">Reorder List</a></p></li><li><p><a href="https://leetcode.com/problems/sort-list/">Sort List</a></p></li><li><p><a href="https://leetcode.com/problems/reverse-nodes-in-k-group/">Reverse Nodes in k-Group</a></p></li></ol><h3><strong>7/ Trees</strong></h3><ol><li><p><a href="https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/">Construct Binary Tree from Preorder and Inorder Traversal</a></p></li><li><p><a href="https://leetcode.com/problems/number-of-good-leaf-nodes-pairs/">Number of Good Leaf Nodes Pairs</a></p></li><li><p><a href="https://leetcode.com/problems/binary-tree-right-side-view/">Binary Tree Right Side View</a></p></li><li><p><a href="https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/">Lowest Common Ancestor of a Binary Tree</a></p></li><li><p><a href="https://leetcode.com/problems/binary-tree-maximum-path-sum/">Binary Tree Maximum Path Sum</a></p></li><li><p><a href="https://leetcode.com/problems/serialize-and-deserialize-binary-tree/">Serialize and Deserialize Binary Tree</a></p></li></ol><h3><strong>8/ Trie</strong></h3><ol><li><p><a href="https://leetcode.com/problems/longest-common-prefix/">Longest Common Prefix</a></p></li><li><p><a href="https://leetcode.com/problems/word-break/">Word Break</a></p></li><li><p><a href="https://leetcode.com/problems/design-add-and-search-words-data-structure/">Design Add And Search Words Data Structure</a></p></li><li><p><a href="https://leetcode.com/problems/word-search-ii/">Word Search II</a></p></li></ol><h3><strong>9/ Backtracking</strong></h3><ol><li><p><a href="https://leetcode.com/problems/word-search/">Word Search</a></p></li><li><p><a href="https://leetcode.com/problems/restore-ip-addresses/">Restore IP Addresses</a></p></li><li><p><a href="http://leetcode.com/problems/generate-parentheses/">Generate Parentheses</a></p></li><li><p><a href="https://leetcode.com/problems/combination-sum/">Combination Sum</a></p></li><li><p><a href="https://leetcode.com/problems/n-queens/">N-Queens</a></p></li><li><p><a href="https://leetcode.com/problems/word-break-ii/">Word Break II</a></p></li></ol><h3><strong>10/ Heap (Priority Queue)</strong></h3><ol><li><p><a href="https://leetcode.com/problems/kth-largest-element-in-an-array/">Kth Largest Element in an Array</a></p></li><li><p><a href="https://leetcode.com/problems/top-k-frequent-elements/">Top K Frequent Elements</a></p></li><li><p><a href="https://leetcode.com/problems/reorganize-string/">Reorganize String</a></p></li><li><p><a href="https://leetcode.com/problems/longest-happy-string/">Longest Happy String</a></p></li><li><p><a href="https://leetcode.com/problems/merge-k-sorted-lists/">Merge k Sorted Lists</a></p></li><li><p><a href="https://leetcode.com/problems/meeting-rooms-iii/">Meeting Rooms III</a></p></li></ol><h3><strong>11/ Graphs</strong></h3><ol><li><p><a href="https://leetcode.com/problems/number-of-islands/">Number of Islands</a></p></li><li><p><a href="https://leetcode.com/problems/course-schedule-ii/">Course Schedule II</a></p></li><li><p><a href="https://leetcode.com/problems/course-schedule/">Course Schedule</a></p></li><li><p><a href="https://leetcode.com/problems/max-area-of-island/">Max Area of Island</a></p></li><li><p><a href="https://leetcode.com/problems/making-a-large-island/">Making A Large Island</a></p></li><li><p><a href="http://leetcode.com/problems/word-ladder/">Word Ladder</a></p></li></ol><h3><strong>12/ Intervals</strong></h3><ol><li><p><a href="https://leetcode.com/problems/insert-interval/">Insert Interval</a></p></li><li><p><a href="https://leetcode.com/problems/merge-intervals/">Merge Intervals</a></p></li><li><p><a href="https://leetcode.com/problems/non-overlapping-intervals/">Non-overlapping Intervals</a></p></li></ol><h3><strong>13/ Greedy &amp; Dynamic Programming</strong></h3><ol><li><p><a href="https://leetcode.com/problems/best-time-to-buy-and-sell-stock/">Best Time to Buy and Sell Stock</a></p></li><li><p><a href="https://leetcode.com/problems/coin-change/">Coin Change</a></p></li><li><p><a href="https://leetcode.com/problems/house-robber/">House Robber</a></p></li><li><p><a href="https://leetcode.com/problems/maximum-swap/">Maximum Swap</a></p></li></ol><h3><strong>14/ Prefix Sum</strong></h3><ol><li><p><a href="https://leetcode.com/problems/subarray-sum-equals-k/">Subarray Sum Equals K</a></p></li><li><p><a href="https://leetcode.com/problems/count-the-hidden-sequences/">Count the Hidden Sequences</a></p></li><li><p><a href="https://leetcode.com/problems/find-the-score-of-all-prefixes-of-an-array/">Find the Score of All Prefixes of an Array</a></p></li></ol><h3><strong>15/ Miscellaneous</strong></h3><ol><li><p><a href="https://leetcode.com/problems/longest-increasing-path-in-a-matrix/">Longest Increasing Path in a Matrix</a></p></li><li><p><a href="https://leetcode.com/problems/bus-routes/">Bus Routes</a></p></li><li><p><a href="https://leetcode.com/problems/shortest-path-in-a-grid-with-obstacles-elimination/">Shortest Path in a Grid with Obstacles Elimination</a></p></li></ol><div><hr></div><p>If you&#8217;re preparing for FAANG in 2026, don&#8217;t forget to save this post.</p><p>Also, here&#8217;s a platform which you can use to practice for your interviews:</p><p><strong>&#128279; Interview Roadmap Platform:</strong> <a href="https://interview-roadmap.vercel.app/">Link</a></p><p>Best of luck!</p>]]></content:encoded></item><item><title><![CDATA[77 LeetCode Problems To Crack Uber]]></title><description><![CDATA[Uber Interview Roadmap]]></description><link>https://singhz.substack.com/p/xx-leetcode-problems-to-crack-uber</link><guid isPermaLink="false">https://singhz.substack.com/p/xx-leetcode-problems-to-crack-uber</guid><dc:creator><![CDATA[Harpreet Singh]]></dc:creator><pubDate>Mon, 25 May 2026 14:01:22 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!Fk51!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba950bc0-09b6-4f52-817e-868073f5088d_1536x1024.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Fk51!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba950bc0-09b6-4f52-817e-868073f5088d_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Fk51!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba950bc0-09b6-4f52-817e-868073f5088d_1536x1024.png 424w, /__u/substackcdn.com/image/fetch/$s_!Fk51!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba950bc0-09b6-4f52-817e-868073f5088d_1536x1024.png 848w, /__u/substackcdn.com/image/fetch/$s_!Fk51!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba950bc0-09b6-4f52-817e-868073f5088d_1536x1024.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Fk51!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba950bc0-09b6-4f52-817e-868073f5088d_1536x1024.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Fk51!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba950bc0-09b6-4f52-817e-868073f5088d_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ba950bc0-09b6-4f52-817e-868073f5088d_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1942062,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/184591980?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba950bc0-09b6-4f52-817e-868073f5088d_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!Fk51!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba950bc0-09b6-4f52-817e-868073f5088d_1536x1024.png 424w, /__u/substackcdn.com/image/fetch/$s_!Fk51!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba950bc0-09b6-4f52-817e-868073f5088d_1536x1024.png 848w, /__u/substackcdn.com/image/fetch/$s_!Fk51!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba950bc0-09b6-4f52-817e-868073f5088d_1536x1024.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Fk51!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba950bc0-09b6-4f52-817e-868073f5088d_1536x1024.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Here are <strong>77 LeetCode Problems</strong> I will solve to crack my <strong>Uber</strong> interview.</p><p>And no, I won&#8217;t &#8220;grind 500 problems.&#8221;</p><p>I will focus on the right ones.</p><p>These are the <strong>exact problems</strong> I will practice right before my <strong>Uber</strong> interview:</p><div><hr></div><h3><strong>1/ Arrays &amp; Hashing</strong></h3><ol><li><p><a href="https://leetcode.com/problems/two-sum/">Two Sum</a></p></li><li><p><a href="https://leetcode.com/problems/valid-anagram/">Valid Anagram</a></p></li><li><p><a href="https://leetcode.com/problems/insert-delete-getrandom-o1/">Insert Delete GetRandom O(1)</a></p></li><li><p><a href="https://leetcode.com/problems/spiral-matrix/">Spiral Matrix</a></p></li><li><p><a href="https://leetcode.com/problems/valid-sudoku/">Valid Sudoku</a></p></li><li><p><a href="https://leetcode.com/problems/group-anagrams/">Group Anagrams</a></p></li><li><p><a href="https://leetcode.com/problems/block-placement-queries/">Block Placement Queries</a></p></li></ol><h3><strong>2/ Two Pointers</strong></h3><ol><li><p><a href="https://leetcode.com/problems/squares-of-a-sorted-array/">Squares of a Sorted Array</a></p></li><li><p><a href="https://leetcode.com/problems/happy-number/">Happy Number</a></p></li><li><p><a href="https://leetcode.com/problems/merge-strings-alternately/">Merge Strings Alternately</a></p></li><li><p><a href="https://leetcode.com/problems/valid-palindrome/">Valid Palindrome</a></p></li><li><p><a href="https://leetcode.com/problems/find-the-distance-value-between-two-arrays/">Find the Distance Value Between Two Arrays</a></p></li><li><p><a href="https://leetcode.com/problems/rotating-the-box/">Rotating the Box</a></p></li></ol><h3><strong>3/ Stack</strong></h3><ol><li><p><a href="https://leetcode.com/problems/exclusive-time-of-functions/">Exclusive Time of Functions</a></p></li><li><p><a href="https://leetcode.com/problems/asteroid-collision/">Asteroid Collision</a></p></li><li><p><a href="https://leetcode.com/problems/min-stack/">Min Stack</a></p></li><li><p><a href="https://leetcode.com/problems/maximum-subarray-min-product/">Maximum Subarray Min-Product</a></p></li><li><p><a href="https://leetcode.com/problems/largest-rectangle-in-histogram/">Largest Rectangle in Histogram</a></p></li><li><p><a href="https://leetcode.com/problems/basic-calculator/">Basic Calculator</a></p></li></ol><h3><strong>4/ Binary Search</strong></h3><ol><li><p><a href="https://leetcode.com/problems/my-calendar-i/">My Calendar I</a></p></li><li><p><a href="https://leetcode.com/problems/random-pick-with-weight/">Random Pick with Weight</a></p></li><li><p><a href="https://leetcode.com/problems/time-based-key-value-store/">Time Based Key-Value Store</a></p></li><li><p><a href="https://leetcode.com/problems/search-in-rotated-sorted-array/">Search in Rotated Sorted Array</a></p></li><li><p><a href="https://leetcode.com/problems/minimum-time-to-complete-trips/">Minimum Time to Complete Trips</a></p></li><li><p><a href="https://leetcode.com/problems/escape-the-spreading-fire/">Escape the Spreading Fire</a></p></li></ol><h3><strong>5/ Sliding Window</strong></h3><ol><li><p><a href="https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/">Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit</a></p></li><li><p><a href="http://leetcode.com/problems/maximize-win-from-two-segments/">Maximize Win From Two Segments</a></p></li><li><p><a href="https://leetcode.com/problems/minimum-operations-to-make-binary-array-elements-equal-to-one-i/">Minimum Operations to Make Binary Array Elements Equal to One I</a></p></li><li><p><a href="https://leetcode.com/problems/count-subarrays-with-fixed-bounds/">Count Subarrays With Fixed Bounds</a></p></li><li><p><a href="https://leetcode.com/problems/minimum-window-substring/">Minimum Window Substring</a></p></li></ol><h3><strong>6/ Linked List</strong></h3><ol><li><p><a href="https://leetcode.com/problems/reverse-linked-list/">Reverse Linked List</a></p></li><li><p><a href="https://leetcode.com/problems/lru-cache/">LRU Cache</a></p></li><li><p><a href="https://leetcode.com/problems/swap-nodes-in-pairs/">Swap Nodes in Pairs</a></p></li><li><p><a href="https://leetcode.com/problems/remove-zero-sum-consecutive-nodes-from-linked-list/">Remove Zero Sum Consecutive Nodes from Linked List</a></p></li><li><p><a href="https://leetcode.com/problems/copy-list-with-random-pointer/">Copy List with Random Pointer</a></p></li><li><p><a href="https://leetcode.com/problems/all-oone-data-structure/">All O`one Data Structure</a></p></li></ol><h3><strong>7/ Trees</strong></h3><ol><li><p><a href="https://leetcode.com/problems/maximum-depth-of-binary-tree/">Maximum Depth of Binary Tree</a></p></li><li><p><a href="https://leetcode.com/problems/construct-quad-tree/">Construct Quad Tree</a></p></li><li><p><a href="https://leetcode.com/problems/kth-smallest-element-in-a-bst/">Kth Smallest Element in a BST</a></p></li><li><p><a href="https://leetcode.com/problems/house-robber-iii/">House Robber III</a></p></li><li><p><a href="https://leetcode.com/problems/delete-node-in-a-bst/">Delete Node in a BST</a></p></li><li><p><a href="https://leetcode.com/problems/serialize-and-deserialize-binary-tree/">Serialize and Deserialize Binary Tree</a></p></li></ol><h3><strong>8/ Trie</strong></h3><ol><li><p><a href="https://leetcode.com/problems/word-break/">Word Break</a></p></li><li><p><a href="https://leetcode.com/problems/replace-words/">Replace Words</a></p></li><li><p><a href="https://leetcode.com/problems/word-search-ii/">Word Search II</a></p></li></ol><h3><strong>9/ Backtracking</strong></h3><ol><li><p><a href="https://leetcode.com/problems/word-search/">Word Search</a></p></li><li><p><a href="https://leetcode.com/problems/letter-combinations-of-a-phone-number/">Letter Combinations of a Phone Number</a></p></li><li><p><a href="https://leetcode.com/problems/subsets/">Subsets</a></p></li><li><p><a href="http://leetcode.com/problems/generate-parentheses/">Generate Parentheses</a></p></li><li><p><a href="https://leetcode.com/problems/sudoku-solver/">Sudoku Solver</a></p></li><li><p><a href="https://leetcode.com/problems/word-break-ii/">Word Break II</a></p></li></ol><h3><strong>10/ Heap (Priority Queue)</strong></h3><ol><li><p><a href="https://leetcode.com/problems/top-k-frequent-words/">Top K Frequent Words</a></p></li><li><p><a href="https://leetcode.com/problems/top-k-frequent-elements/">Top K Frequent Elements</a></p></li><li><p><a href="https://leetcode.com/problems/find-k-pairs-with-smallest-sums/">Find K Pairs with Smallest Sums</a></p></li><li><p><a href="https://leetcode.com/problems/maximum-number-of-eaten-apples/">Maximum Number of Eaten Apples</a></p></li><li><p><a href="https://leetcode.com/problems/merge-k-sorted-lists/">Merge k Sorted Lists</a></p></li><li><p><a href="https://leetcode.com/problems/meeting-rooms-iii/">Meeting Rooms III</a></p></li></ol><h3><strong>11/ Graphs</strong></h3><ol><li><p><a href="https://leetcode.com/problems/number-of-islands/">Number of Islands</a></p></li><li><p><a href="https://leetcode.com/problems/evaluate-division/">Evaluate Division</a></p></li><li><p><a href="https://leetcode.com/problems/course-schedule-ii/">Course Schedule II</a></p></li><li><p><a href="https://leetcode.com/problems/shortest-bridge/">Shortest Bridge</a></p></li><li><p><a href="https://leetcode.com/problems/course-schedule/">Course Schedule</a></p></li><li><p><a href="https://leetcode.com/problems/bus-routes/">Bus Routes</a></p></li></ol><h3><strong>12/ Intervals</strong></h3><ol><li><p><a href="https://leetcode.com/problems/insert-interval/">Insert Interval</a></p></li><li><p><a href="https://leetcode.com/problems/interval-list-intersections/">Interval List Intersections</a></p></li><li><p><a href="https://leetcode.com/problems/count-integers-in-intervals/">Count Integers in Intervals</a></p></li></ol><h3><strong>13/ Greedy &amp; Dynamic Programming</strong></h3><ol><li><p><a href="https://leetcode.com/problems/best-time-to-buy-and-sell-stock/">Best Time to Buy and Sell Stock</a></p></li><li><p><a href="https://leetcode.com/problems/decode-ways/">Decode Ways</a></p></li><li><p><a href="https://leetcode.com/problems/cherry-pickup/">Cherry Pickup</a></p></li><li><p><a href="https://leetcode.com/problems/text-justification/">Text Justification</a></p></li></ol><h3><strong>14/ Prefix Sum</strong></h3><ol><li><p><a href="https://leetcode.com/problems/product-of-array-except-self/">Product of Array Except Self</a></p></li><li><p><a href="https://leetcode.com/problems/subarray-sums-divisible-by-k/">Subarray Sums Divisible by K</a></p></li><li><p><a href="https://leetcode.com/problems/subarray-sum-equals-k/">Subarray Sum Equals K</a></p></li></ol><h3><strong>15/ Miscellaneous</strong></h3><ol><li><p><a href="https://leetcode.com/problems/shuffle-an-array/">Shuffle an Array</a></p></li><li><p><a href="https://leetcode.com/problems/find-the-closest-palindrome/">Find the Closest Palindrome</a></p></li><li><p><a href="https://leetcode.com/problems/split-message-based-on-limit/">Split Message Based on Limit</a></p></li><li><p><a href="https://leetcode.com/problems/making-a-large-island/">Making A Large Island</a></p></li></ol><div><hr></div><p>If you&#8217;re preparing for FAANG in 2026, don&#8217;t forget to save this post.</p><p>Also, here&#8217;s a platform which you can use to practice for your interviews:</p><p><strong>&#128279; Interview Roadmap Platform:</strong> <a href="https://interview-roadmap.vercel.app/">Link</a></p><p>Best of luck!</p>]]></content:encoded></item><item><title><![CDATA[77 LeetCode Problems To Crack Bloomberg]]></title><description><![CDATA[Bloomberg Interview Roadmap]]></description><link>https://singhz.substack.com/p/xx-leetcode-problems-to-crack-bloomberg</link><guid isPermaLink="false">https://singhz.substack.com/p/xx-leetcode-problems-to-crack-bloomberg</guid><dc:creator><![CDATA[Harpreet Singh]]></dc:creator><pubDate>Mon, 18 May 2026 14:02:06 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!5aPL!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b92c859-5d51-4d7b-8ef3-8457c31b1ab2_1536x1024.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!5aPL!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b92c859-5d51-4d7b-8ef3-8457c31b1ab2_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!5aPL!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b92c859-5d51-4d7b-8ef3-8457c31b1ab2_1536x1024.png 424w, /__u/substackcdn.com/image/fetch/$s_!5aPL!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b92c859-5d51-4d7b-8ef3-8457c31b1ab2_1536x1024.png 848w, /__u/substackcdn.com/image/fetch/$s_!5aPL!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b92c859-5d51-4d7b-8ef3-8457c31b1ab2_1536x1024.png 1272w, /__u/substackcdn.com/image/fetch/$s_!5aPL!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b92c859-5d51-4d7b-8ef3-8457c31b1ab2_1536x1024.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!5aPL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b92c859-5d51-4d7b-8ef3-8457c31b1ab2_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7b92c859-5d51-4d7b-8ef3-8457c31b1ab2_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1852954,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/184591445?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b92c859-5d51-4d7b-8ef3-8457c31b1ab2_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!5aPL!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b92c859-5d51-4d7b-8ef3-8457c31b1ab2_1536x1024.png 424w, /__u/substackcdn.com/image/fetch/$s_!5aPL!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b92c859-5d51-4d7b-8ef3-8457c31b1ab2_1536x1024.png 848w, /__u/substackcdn.com/image/fetch/$s_!5aPL!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b92c859-5d51-4d7b-8ef3-8457c31b1ab2_1536x1024.png 1272w, /__u/substackcdn.com/image/fetch/$s_!5aPL!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b92c859-5d51-4d7b-8ef3-8457c31b1ab2_1536x1024.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Here are <strong>77 LeetCode Problems</strong> I will solve to crack my <strong>Bloomberg</strong> interview.</p><p>And no, I won&#8217;t &#8220;grind 500 problems.&#8221;</p><p>I will focus on the right ones.</p><p>These are the <strong>exact problems</strong> I will practice right before my <strong>Bloomberg</strong> interview:</p><div><hr></div><h3><strong>1/ Arrays &amp; Hashing</strong></h3><ol><li><p><a href="https://leetcode.com/problems/remove-letter-to-equalize-frequency/">Remove Letter To Equalize Frequency</a></p></li><li><p><a href="https://leetcode.com/problems/two-sum/">Two Sum</a></p></li><li><p><a href="https://leetcode.com/problems/roman-to-integer/">Roman to Integer</a></p></li><li><p><a href="https://leetcode.com/problems/valid-anagram/">Valid Anagram</a></p></li><li><p><a href="https://leetcode.com/problems/insert-delete-getrandom-o1/">Insert Delete GetRandom O(1)</a></p></li><li><p><a href="https://leetcode.com/problems/invalid-transactions/">Invalid Transactions</a></p></li><li><p><a href="https://leetcode.com/problems/design-underground-system/">Design Underground System</a></p></li><li><p><a href="https://leetcode.com/problems/group-anagrams/">Group Anagrams</a></p></li></ol><h3><strong>2/ Two Pointers</strong></h3><ol><li><p><a href="https://leetcode.com/problems/remove-duplicates-from-sorted-array/">Remove Duplicates from Sorted Array</a></p></li><li><p><a href="https://leetcode.com/problems/container-with-most-water/">Container With Most Water</a></p></li><li><p><a href="https://leetcode.com/problems/3sum/">3Sum</a></p></li><li><p><a href="https://leetcode.com/problems/longest-palindromic-substring/">Longest Palindromic Substring</a></p></li><li><p><a href="https://leetcode.com/problems/valid-triangle-number/">Valid Triangle Number</a></p></li><li><p><a href="https://leetcode.com/problems/trapping-rain-water/">Trapping Rain Water</a></p></li></ol><h3><strong>3/ Stack</strong></h3><ol><li><p><a href="https://leetcode.com/problems/valid-parentheses/">Valid Parentheses</a></p></li><li><p><a href="https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses/">Maximum Nesting Depth of the Parentheses</a></p></li><li><p><a href="https://leetcode.com/problems/decode-string/">Decode String</a></p></li><li><p><a href="https://leetcode.com/problems/min-stack/">Min Stack</a></p></li><li><p><a href="https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/">Remove All Adjacent Duplicates in String II</a></p></li><li><p><a href="https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/">Minimum Remove to Make Valid Parentheses</a></p></li><li><p><a href="https://leetcode.com/problems/design-browser-history/">Design Browser History</a></p></li><li><p><a href="https://leetcode.com/problems/basic-calculator-ii/">Basic Calculator II</a></p></li></ol><h3><strong>4/ Binary Search</strong></h3><ol><li><p><a href="https://leetcode.com/problems/search-insert-position/">Search Insert Position</a></p></li><li><p><a href="https://leetcode.com/problems/sqrtx/">Sqrt(x)</a></p></li><li><p><a href="https://leetcode.com/problems/search-in-rotated-sorted-array/">Search in Rotated Sorted Array</a></p></li><li><p><a href="https://leetcode.com/problems/search-a-2d-matrix/">Search a 2D Matrix</a></p></li><li><p><a href="https://leetcode.com/problems/peak-index-in-a-mountain-array/">Peak Index in a Mountain Array</a></p></li><li><p><a href="https://leetcode.com/problems/median-of-two-sorted-arrays/">Median of Two Sorted Arrays</a></p></li></ol><h3><strong>5/ Sliding Window</strong></h3><ol><li><p><a href="https://leetcode.com/problems/contains-duplicate-ii/">Contains Duplicate II</a></p></li><li><p><a href="https://leetcode.com/problems/maximum-average-subarray-i/">Maximum Average Subarray I</a></p></li><li><p><a href="https://leetcode.com/problems/find-x-sum-of-all-k-long-subarrays-i/">Find X-Sum of All K-Long Subarrays I</a></p></li><li><p><a href="https://leetcode.com/problems/longest-substring-without-repeating-characters/">Longest Substring Without Repeating Characters</a></p></li><li><p><a href="https://leetcode.com/problems/fruit-into-baskets/">Fruit Into Baskets</a></p></li><li><p><a href="https://leetcode.com/problems/minimum-size-subarray-sum/">Minimum Size Subarray Sum</a></p></li></ol><h3><strong>6/ Linked List</strong></h3><ol><li><p><a href="https://leetcode.com/problems/merge-two-sorted-lists/">Merge Two Sorted Lists</a></p></li><li><p><a href="https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/">Flatten a Multilevel Doubly Linked List</a></p></li><li><p><a href="https://leetcode.com/problems/add-two-numbers/">Add Two Numbers</a></p></li><li><p><a href="https://leetcode.com/problems/lru-cache/">LRU Cache</a></p></li><li><p><a href="https://leetcode.com/problems/reorder-list/">Reorder List</a></p></li><li><p><a href="https://leetcode.com/problems/rotate-list/">Rotate List</a></p></li></ol><h3><strong>7/ Trees</strong></h3><ol><li><p><a href="https://leetcode.com/problems/binary-tree-inorder-traversal/">Binary Tree Inorder Traversal</a></p></li><li><p><a href="https://leetcode.com/problems/validate-binary-search-tree/">Validate Binary Search Tree</a></p></li><li><p><a href="https://leetcode.com/problems/flatten-binary-tree-to-linked-list/">Flatten Binary Tree to Linked List</a></p></li><li><p><a href="https://leetcode.com/problems/binary-tree-level-order-traversal/">Binary Tree Level Order Traversal</a></p></li><li><p><a href="https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/">Construct Binary Tree from Preorder and Inorder Traversal</a></p></li><li><p><a href="https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree/">Vertical Order Traversal of a Binary Tree</a></p></li></ol><h3><strong>8/ Trie</strong></h3><ol><li><p><a href="https://leetcode.com/problems/longest-common-prefix/">Longest Common Prefix</a></p></li><li><p><a href="https://leetcode.com/problems/implement-magic-dictionary/">Implement Magic Dictionary</a></p></li><li><p><a href="https://leetcode.com/problems/word-break/">Word Break</a></p></li><li><p><a href="https://leetcode.com/problems/implement-trie-prefix-tree/">Implement Trie (Prefix Tree)</a></p></li></ol><h3><strong>9/ Backtracking</strong></h3><ol><li><p><a href="https://leetcode.com/problems/word-search/">Word Search</a></p></li><li><p><a href="https://leetcode.com/problems/all-paths-from-source-to-target/">All Paths From Source to Target</a></p></li><li><p><a href="https://leetcode.com/problems/next-greater-numerically-balanced-number/">Next Greater Numerically Balanced Number</a></p></li><li><p><a href="https://leetcode.com/problems/letter-combinations-of-a-phone-number/">Letter Combinations of a Phone Number</a></p></li><li><p><a href="https://leetcode.com/problems/permutations-ii/">Permutations II</a></p></li><li><p><a href="https://leetcode.com/problems/word-break-ii/">Word Break II</a></p></li></ol><h3><strong>10/ Heap (Priority Queue)</strong></h3><ol><li><p><a href="https://leetcode.com/problems/relative-ranks/">Relative Ranks</a></p></li><li><p><a href="https://leetcode.com/problems/top-k-frequent-elements/">Top K Frequent Elements</a></p></li><li><p><a href="https://leetcode.com/problems/top-k-frequent-words/">Top K Frequent Words</a></p></li><li><p><a href="https://leetcode.com/problems/merge-k-sorted-lists/">Merge k Sorted Lists</a></p></li><li><p><a href="https://leetcode.com/problems/find-median-from-data-stream/">Find Median from Data Stream</a></p></li><li><p><a href="https://leetcode.com/problems/ipo/">IPO</a></p></li></ol><h3><strong>11/ Graphs</strong></h3><ol><li><p><a href="https://leetcode.com/problems/evaluate-division/">Evaluate Division</a></p></li><li><p><a href="https://leetcode.com/problems/course-schedule-ii/">Course Schedule II</a></p></li><li><p><a href="https://leetcode.com/problems/number-of-islands/">Number of Islands</a></p></li><li><p><a href="https://leetcode.com/problems/rotting-oranges/">Rotting Oranges</a></p></li><li><p><a href="https://leetcode.com/problems/shortest-path-in-binary-matrix/">Shortest Path in Binary Matrix</a></p></li></ol><h3><strong>12/ Intervals</strong></h3><ol><li><p><a href="https://leetcode.com/problems/merge-intervals/">Merge Intervals</a></p></li></ol><h3><strong>13/ Greedy &amp; Dynamic Programming</strong></h3><ol><li><p><a href="https://leetcode.com/problems/best-time-to-buy-and-sell-stock/">Best Time to Buy and Sell Stock</a></p></li><li><p><a href="https://leetcode.com/problems/sort-integers-by-the-power-value/">Sort Integers by The Power Value</a></p></li><li><p><a href="https://leetcode.com/problems/maximum-subarray/">Maximum Subarray</a></p></li></ol><h3><strong>14/ Prefix Sum</strong></h3><ol><li><p><a href="https://leetcode.com/problems/subarray-sum-equals-k/">Subarray Sum Equals K</a></p></li><li><p><a href="https://leetcode.com/problems/product-of-array-except-self/">Product of Array Except Self</a></p></li><li><p><a href="https://leetcode.com/problems/continuous-subarray-sum/">Continuous Subarray Sum</a></p></li></ol><h3><strong>15/ Miscellaneous</strong></h3><ol><li><p><a href="https://leetcode.com/problems/palindrome-number/">Palindrome Number</a></p></li><li><p><a href="https://leetcode.com/problems/plus-one/">Plus One</a></p></li><li><p><a href="https://leetcode.com/problems/reverse-integer/">Reverse Integer</a></p></li></ol><div><hr></div><p>If you&#8217;re preparing for FAANG in 2026, don&#8217;t forget to save this post.</p><p>Also, here&#8217;s a platform which you can use to practice for your interviews:</p><p><strong>&#128279; Interview Roadmap Platform:</strong> <a href="https://interview-roadmap.vercel.app/">Link</a></p><p>Best of luck!</p>]]></content:encoded></item><item><title><![CDATA[75 LeetCode Problems To Crack Microsoft]]></title><description><![CDATA[Microsoft Interview Roadmap]]></description><link>https://singhz.substack.com/p/79-leetcode-problems-to-crack-microsoft</link><guid isPermaLink="false">https://singhz.substack.com/p/79-leetcode-problems-to-crack-microsoft</guid><dc:creator><![CDATA[Harpreet Singh]]></dc:creator><pubDate>Mon, 11 May 2026 14:01:37 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!qvJj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcf6d6ed-c27a-4ed3-88e7-1697f9a4b2c6_1536x1024.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!qvJj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcf6d6ed-c27a-4ed3-88e7-1697f9a4b2c6_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!qvJj!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcf6d6ed-c27a-4ed3-88e7-1697f9a4b2c6_1536x1024.png 424w, /__u/substackcdn.com/image/fetch/$s_!qvJj!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcf6d6ed-c27a-4ed3-88e7-1697f9a4b2c6_1536x1024.png 848w, /__u/substackcdn.com/image/fetch/$s_!qvJj!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcf6d6ed-c27a-4ed3-88e7-1697f9a4b2c6_1536x1024.png 1272w, /__u/substackcdn.com/image/fetch/$s_!qvJj!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcf6d6ed-c27a-4ed3-88e7-1697f9a4b2c6_1536x1024.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!qvJj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcf6d6ed-c27a-4ed3-88e7-1697f9a4b2c6_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bcf6d6ed-c27a-4ed3-88e7-1697f9a4b2c6_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1886976,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/184494945?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcf6d6ed-c27a-4ed3-88e7-1697f9a4b2c6_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!qvJj!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcf6d6ed-c27a-4ed3-88e7-1697f9a4b2c6_1536x1024.png 424w, /__u/substackcdn.com/image/fetch/$s_!qvJj!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcf6d6ed-c27a-4ed3-88e7-1697f9a4b2c6_1536x1024.png 848w, /__u/substackcdn.com/image/fetch/$s_!qvJj!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcf6d6ed-c27a-4ed3-88e7-1697f9a4b2c6_1536x1024.png 1272w, /__u/substackcdn.com/image/fetch/$s_!qvJj!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcf6d6ed-c27a-4ed3-88e7-1697f9a4b2c6_1536x1024.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Here are <strong>75 LeetCode Problems</strong> I will solve to crack my <strong>Microsoft</strong> interview.</p><p>And no, I won&#8217;t &#8220;grind 500 problems.&#8221;</p><p>I will focus on the right ones.</p><p>These are the <strong>exact problems</strong> I will practice right before my <strong>Microsoft</strong> interview:</p><div><hr></div><h3><strong>1/ Arrays &amp; Hashing</strong></h3><ol><li><p><a href="https://leetcode.com/problems/two-sum/">Two Sum</a></p></li><li><p><a href="https://leetcode.com/problems/roman-to-integer/">Roman to Integer</a></p></li><li><p><a href="https://leetcode.com/problems/valid-anagram/">Valid Anagram</a></p></li><li><p><a href="https://leetcode.com/problems/group-anagrams/">Group Anagrams</a></p></li><li><p><a href="https://leetcode.com/problems/smallest-string-with-swaps/">Smallest String With Swaps</a></p></li><li><p><a href="https://leetcode.com/problems/spiral-matrix/">Spiral Matrix</a></p></li></ol><h3><strong>2/ Two Pointers</strong></h3><ol><li><p><a href="https://leetcode.com/problems/remove-duplicates-from-sorted-array/">Remove Duplicates from Sorted Array</a></p></li><li><p><a href="https://leetcode.com/problems/longest-palindromic-substring/">Longest Palindromic Substring</a></p></li><li><p><a href="https://leetcode.com/problems/container-with-most-water/">Container With Most Water</a></p></li><li><p><a href="https://leetcode.com/problems/3sum/">3Sum</a></p></li><li><p><a href="https://leetcode.com/problems/sort-colors/">Sort Colors</a></p></li><li><p><a href="https://leetcode.com/problems/trapping-rain-water/">Trapping Rain Water</a></p></li></ol><h3><strong>3/ Stack</strong></h3><ol><li><p><a href="https://leetcode.com/problems/valid-parentheses/">Valid Parentheses</a></p></li><li><p><a href="https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/">Final Prices With a Special Discount in a Shop</a></p></li><li><p><a href="https://leetcode.com/problems/next-greater-element-i/">Next Greater Element I</a></p></li><li><p><a href="https://leetcode.com/problems/decode-string/">Decode String</a></p></li><li><p><a href="https://leetcode.com/problems/asteroid-collision/">Asteroid Collision</a></p></li><li><p><a href="https://leetcode.com/problems/online-stock-span/">Online Stock Span</a></p></li></ol><h3><strong>4/ Binary Search</strong></h3><ol><li><p><a href="https://leetcode.com/problems/search-in-rotated-sorted-array/">Search in Rotated Sorted Array</a></p></li><li><p><a href="https://leetcode.com/problems/find-peak-element/">Find Peak Element</a></p></li><li><p><a href="http://leetcode.com/problems/find-minimum-in-rotated-sorted-array/">Find Minimum in Rotated Sorted Array</a></p></li><li><p><a href="https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/">Find First and Last Position of Element in Sorted Array</a></p></li><li><p><a href="https://leetcode.com/problems/search-a-2d-matrix/">Search a 2D Matrix</a></p></li><li><p><a href="https://leetcode.com/problems/median-of-two-sorted-arrays/">Median of Two Sorted Arrays</a></p></li></ol><h3><strong>5/ Sliding Window</strong></h3><ol><li><p><a href="https://leetcode.com/problems/longest-nice-substring/">Longest Nice Substring</a></p></li><li><p><a href="https://leetcode.com/problems/longest-substring-without-repeating-characters/">Longest Substring Without Repeating Characters</a></p></li><li><p><a href="https://leetcode.com/problems/longest-repeating-character-replacement/">Longest Repeating Character Replacement</a></p></li><li><p><a href="https://leetcode.com/problems/permutation-in-string/">Permutation in String</a></p></li><li><p><a href="https://leetcode.com/problems/minimum-operations-to-make-binary-array-elements-equal-to-one-i/">Minimum Operations to Make Binary Array Elements Equal to One I</a></p></li><li><p><a href="https://leetcode.com/problems/sliding-window-maximum/">Sliding Window Maximum</a></p></li></ol><h3><strong>6/ Linked List</strong></h3><ol><li><p><a href="https://leetcode.com/problems/merge-two-sorted-lists/">Merge Two Sorted Lists</a></p></li><li><p><a href="https://leetcode.com/problems/add-two-numbers/">Add Two Numbers</a></p></li><li><p><a href="https://leetcode.com/problems/design-authentication-manager/">Design Authentication Manager</a></p></li><li><p><a href="https://leetcode.com/problems/lru-cache/">LRU Cache</a></p></li><li><p><a href="https://leetcode.com/problems/remove-nth-node-from-end-of-list/">Remove Nth Node From End of List</a></p></li><li><p><a href="https://leetcode.com/problems/copy-list-with-random-pointer/">Copy List with Random Pointer</a></p></li></ol><h3><strong>7/ Trees</strong></h3><ol><li><p><a href="https://leetcode.com/problems/symmetric-tree/">Symmetric Tree</a></p></li><li><p><a href="https://leetcode.com/problems/same-tree/">Same Tree</a></p></li><li><p><a href="https://leetcode.com/problems/maximum-depth-of-binary-tree/">Maximum Depth of Binary Tree</a></p></li><li><p><a href="https://leetcode.com/problems/recover-binary-search-tree/">Recover Binary Search Tree</a></p></li><li><p><a href="https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/">Lowest Common Ancestor of a Binary Tree</a></p></li><li><p><a href="https://leetcode.com/problems/serialize-and-deserialize-binary-tree/">Serialize and Deserialize Binary Tree</a></p></li></ol><h3><strong>8/ Trie</strong></h3><ol><li><p><a href="https://leetcode.com/problems/longest-common-prefix/">Longest Common Prefix</a></p></li><li><p><a href="https://leetcode.com/problems/design-add-and-search-words-data-structure/">Design Add And Search Words Data Structure</a></p></li><li><p><a href="https://leetcode.com/problems/implement-trie-prefix-tree/">Implement Trie (Prefix Tree)</a></p></li></ol><h3><strong>9/ Backtracking</strong></h3><ol><li><p><a href="https://leetcode.com/problems/letter-combinations-of-a-phone-number/">Letter Combinations of a Phone Number</a></p></li><li><p><a href="https://leetcode.com/problems/generate-parentheses/">Generate Parentheses</a></p></li><li><p><a href="https://leetcode.com/problems/word-search/">Word Search</a></p></li><li><p><a href="https://leetcode.com/problems/restore-ip-addresses/">Restore IP Addresses</a></p></li><li><p><a href="https://leetcode.com/problems/subsets/">Subsets</a></p></li><li><p><a href="https://leetcode.com/problems/n-queens/">N-Queens</a></p></li></ol><h3><strong>10/ Heap (Priority Queue)</strong></h3><ol><li><p><a href="https://leetcode.com/problems/kth-largest-element-in-an-array/">Kth Largest Element in an Array</a></p></li><li><p><a href="https://leetcode.com/problems/top-k-frequent-elements/">Top K Frequent Elements</a></p></li><li><p><a href="https://leetcode.com/problems/reorganize-string/">Reorganize String</a></p></li><li><p><a href="https://leetcode.com/problems/cheapest-flights-within-k-stops/">Cheapest Flights Within K Stops</a></p></li><li><p><a href="https://leetcode.com/problems/task-scheduler/">Task Scheduler</a></p></li><li><p><a href="https://leetcode.com/problems/merge-k-sorted-lists/">Merge k Sorted Lists</a></p></li></ol><h3><strong>11/ Graphs</strong></h3><ol><li><p><a href="https://leetcode.com/problems/course-schedule/">Course Schedule</a></p></li><li><p><a href="https://leetcode.com/problems/course-schedule-ii/">Course Schedule II</a></p></li><li><p><a href="https://leetcode.com/problems/number-of-provinces/">Number of Provinces</a></p></li><li><p><a href="https://leetcode.com/problems/is-graph-bipartite/">Is Graph Bipartite?</a></p></li><li><p><a href="https://leetcode.com/problems/number-of-islands/">Number of Islands</a></p></li><li><p><a href="https://leetcode.com/problems/minimum-edge-reversals-so-every-node-is-reachable/">Minimum Edge Reversals So Every Node Is Reachable</a></p></li></ol><h3><strong>12/ Intervals</strong></h3><ol><li><p><a href="https://leetcode.com/problems/merge-intervals/">Merge Intervals</a></p></li><li><p><a href="https://leetcode.com/problems/insert-interval/">Insert Interval</a></p></li></ol><h3><strong>13/ Greedy &amp; Dynamic Programming</strong></h3><ol><li><p><a href="https://leetcode.com/problems/best-time-to-buy-and-sell-stock/">Best Time to Buy and Sell Stock</a></p></li><li><p><a href="https://leetcode.com/problems/climbing-stairs/">Climbing Stairs</a></p></li><li><p><a href="https://leetcode.com/problems/maximum-subarray/">Maximum Subarray</a></p></li><li><p><a href="https://leetcode.com/problems/longest-palindromic-subsequence/">Longest Palindromic Subsequence</a></p></li></ol><h3><strong>14/ Prefix Sum</strong></h3><ol><li><p><a href="https://leetcode.com/problems/subarray-sum-equals-k/">Subarray Sum Equals K</a></p></li><li><p><a href="https://leetcode.com/problems/product-of-array-except-self/">Product of Array Except Self</a></p></li><li><p><a href="https://leetcode.com/problems/shifting-letters/">Shifting Letters</a></p></li></ol><h3><strong>15/ Miscellaneous</strong></h3><ol><li><p><a href="https://leetcode.com/problems/palindrome-number/">Palindrome Number</a></p></li><li><p><a href="https://leetcode.com/problems/reverse-integer/">Reverse Integer</a></p></li><li><p><a href="https://leetcode.com/problems/next-permutation/">Next Permutation</a></p></li></ol><div><hr></div><p>If you&#8217;re preparing for FAANG in 2026, don&#8217;t forget to save this post.</p><p>Also, here&#8217;s a platform which you can use to practice for your interviews:</p><p><strong>&#128279; Interview Roadmap Platform:</strong> <a href="https://interview-roadmap.vercel.app/">Link</a></p><p>Best of luck!</p>]]></content:encoded></item><item><title><![CDATA[76 LeetCode Problems To Crack Meta]]></title><description><![CDATA[Meta Interview Roadmap]]></description><link>https://singhz.substack.com/p/77-leetcode-problems-to-crack-meta</link><guid isPermaLink="false">https://singhz.substack.com/p/77-leetcode-problems-to-crack-meta</guid><dc:creator><![CDATA[Harpreet Singh]]></dc:creator><pubDate>Mon, 04 May 2026 14:03:06 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!XbnW!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9f4170b-e41a-4e87-a78b-ab542617ff61_1536x1024.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!XbnW!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9f4170b-e41a-4e87-a78b-ab542617ff61_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!XbnW!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9f4170b-e41a-4e87-a78b-ab542617ff61_1536x1024.png 424w, /__u/substackcdn.com/image/fetch/$s_!XbnW!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9f4170b-e41a-4e87-a78b-ab542617ff61_1536x1024.png 848w, /__u/substackcdn.com/image/fetch/$s_!XbnW!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9f4170b-e41a-4e87-a78b-ab542617ff61_1536x1024.png 1272w, /__u/substackcdn.com/image/fetch/$s_!XbnW!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9f4170b-e41a-4e87-a78b-ab542617ff61_1536x1024.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!XbnW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9f4170b-e41a-4e87-a78b-ab542617ff61_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e9f4170b-e41a-4e87-a78b-ab542617ff61_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1794019,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/184489326?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9f4170b-e41a-4e87-a78b-ab542617ff61_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!XbnW!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9f4170b-e41a-4e87-a78b-ab542617ff61_1536x1024.png 424w, /__u/substackcdn.com/image/fetch/$s_!XbnW!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9f4170b-e41a-4e87-a78b-ab542617ff61_1536x1024.png 848w, /__u/substackcdn.com/image/fetch/$s_!XbnW!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9f4170b-e41a-4e87-a78b-ab542617ff61_1536x1024.png 1272w, /__u/substackcdn.com/image/fetch/$s_!XbnW!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9f4170b-e41a-4e87-a78b-ab542617ff61_1536x1024.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Here are <strong>76 LeetCode Problems</strong> I will solve to crack my <strong>Meta</strong> interview.</p><p>And no, I won&#8217;t &#8220;grind 500 problems.&#8221;</p><p>I will focus on the right ones.</p><p>These are the <strong>exact problems</strong> I will practice right before my <strong>Meta</strong> interview:</p><div><hr></div><h3><strong>1/ Arrays &amp; Hashing</strong></h3><ol><li><p><a href="https://leetcode.com/problems/two-sum/">Two Sum</a></p></li><li><p><a href="https://leetcode.com/problems/contains-duplicate/">Contains Duplicate</a></p></li><li><p><a href="https://leetcode.com/problems/roman-to-integer/">Roman to Integer</a></p></li><li><p><a href="https://leetcode.com/problems/simple-bank-system/">Simple Bank System</a></p></li><li><p><a href="https://leetcode.com/problems/custom-sort-string/">Custom Sort String</a></p></li><li><p><a href="https://leetcode.com/problems/group-anagrams/">Group Anagrams</a></p></li></ol><h3><strong>2/ Two Pointers</strong></h3><ol><li><p><a href="https://leetcode.com/problems/valid-palindrome-ii/">Valid Palindrome II</a></p></li><li><p><a href="https://leetcode.com/problems/merge-sorted-array/">Merge Sorted Array</a></p></li><li><p><a href="https://leetcode.com/problems/valid-palindrome/">Valid Palindrome</a></p></li><li><p><a href="https://leetcode.com/problems/longest-palindromic-substring/">Longest Palindromic Substring</a></p></li><li><p><a href="https://leetcode.com/problems/container-with-most-water/">Container With Most Water</a></p></li><li><p><a href="https://leetcode.com/problems/3sum/">3Sum</a></p></li></ol><h3><strong>3/ Stack</strong></h3><ol><li><p><a href="https://leetcode.com/problems/valid-parentheses/">Valid Parentheses</a></p></li><li><p><a href="http://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/">Remove All Adjacent Duplicates In String</a></p></li><li><p><a href="https://leetcode.com/problems/remove-outermost-parentheses/">Remove Outermost Parentheses</a></p></li><li><p><a href="https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/">Minimum Remove to Make Valid Parentheses</a></p></li><li><p><a href="https://leetcode.com/problems/simplify-path/">Simplify Path</a></p></li><li><p><a href="https://leetcode.com/problems/basic-calculator-ii/">Basic Calculator II</a></p></li></ol><h3><strong>4/ Binary Search</strong></h3><ol><li><p><a href="https://leetcode.com/problems/kth-missing-positive-number/">Kth Missing Positive Number</a></p></li><li><p><a href="https://leetcode.com/problems/binary-search/">Binary Search</a></p></li><li><p><a href="https://leetcode.com/problems/search-insert-position/">Search Insert Position</a></p></li><li><p><a href="https://leetcode.com/problems/random-pick-with-weight/">Random Pick with Weight</a></p></li><li><p><a href="https://leetcode.com/problems/find-peak-element/">Find Peak Element</a></p></li><li><p><a href="https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/">Find First and Last Position of Element in Sorted Array</a></p></li></ol><h3><strong>5/ Sliding Window</strong></h3><ol><li><p><a href="https://leetcode.com/problems/contains-duplicate-ii/">Contains Duplicate II</a></p></li><li><p><a href="https://leetcode.com/problems/maximum-average-subarray-i/">Maximum Average Subarray I</a></p></li><li><p><a href="https://leetcode.com/problems/find-the-k-beauty-of-a-number/">Find the K-Beauty of a Number</a></p></li><li><p><a href="https://leetcode.com/problems/longest-substring-without-repeating-characters/">Longest Substring Without Repeating Characters</a></p></li><li><p><a href="https://leetcode.com/problems/max-consecutive-ones-iii/">Max Consecutive Ones III</a></p></li><li><p><a href="https://leetcode.com/problems/minimum-window-substring/">Minimum Window Substring</a></p></li></ol><h3><strong>6/ Linked List</strong></h3><ol><li><p><a href="https://leetcode.com/problems/intersection-of-two-linked-lists/">Intersection of Two Linked Lists</a></p></li><li><p><a href="https://leetcode.com/problems/reverse-linked-list/">Reverse Linked List</a></p></li><li><p><a href="https://leetcode.com/problems/remove-duplicates-from-sorted-list/">Remove Duplicates from Sorted List</a></p></li><li><p><a href="https://leetcode.com/problems/add-two-numbers/">Add Two Numbers</a></p></li><li><p><a href="https://leetcode.com/problems/lru-cache/">LRU Cache</a></p></li><li><p><a href="https://leetcode.com/problems/copy-list-with-random-pointer/">Copy List with Random Pointer</a></p></li></ol><h3><strong>7/ Trees</strong></h3><ol><li><p><a href="https://leetcode.com/problems/diameter-of-binary-tree/">Diameter of Binary Tree</a></p></li><li><p><a href="https://leetcode.com/problems/range-sum-of-bst/">Range Sum of BST</a></p></li><li><p><a href="https://leetcode.com/problems/same-tree/">Same Tree</a></p></li><li><p><a href="https://leetcode.com/problems/binary-tree-right-side-view/">Binary Tree Right Side View</a></p></li><li><p><a href="https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/">Lowest Common Ancestor of a Binary Tree</a></p></li><li><p><a href="https://leetcode.com/problems/sum-root-to-leaf-numbers/">Sum Root to Leaf Numbers</a></p></li></ol><h3><strong>8/ Trie</strong></h3><ol><li><p><a href="https://leetcode.com/problems/longest-common-prefix/">Longest Common Prefix</a></p></li><li><p><a href="https://leetcode.com/problems/count-prefix-and-suffix-pairs-i/">Count Prefix and Suffix Pairs I</a></p></li><li><p><a href="https://leetcode.com/problems/word-break/">Word Break</a></p></li><li><p><a href="https://leetcode.com/problems/implement-trie-prefix-tree/">Implement Trie (Prefix Tree)</a></p></li><li><p><a href="https://leetcode.com/problems/design-add-and-search-words-data-structure/">Design Add And Search Words Data Structure</a></p></li></ol><h3><strong>9/ Backtracking</strong></h3><ol><li><p><a href="https://leetcode.com/problems/binary-tree-paths/">Binary Tree Paths</a></p></li><li><p><a href="https://leetcode.com/problems/binary-watch/">Binary Watch</a></p></li><li><p><a href="https://leetcode.com/problems/sum-of-all-subset-xor-totals/">Sum of All Subset XOR Totals</a></p></li><li><p><a href="https://leetcode.com/problems/generate-parentheses/">Generate Parentheses</a></p></li><li><p><a href="https://leetcode.com/problems/subsets/">Subsets</a></p></li><li><p><a href="https://leetcode.com/problems/combination-sum/">Combination Sum</a></p></li></ol><h3><strong>10/ Heap (Priority Queue)</strong></h3><ol><li><p><a href="https://leetcode.com/problems/last-stone-weight/">Last Stone Weight</a></p></li><li><p><a href="https://leetcode.com/problems/kth-largest-element-in-a-stream/">Kth Largest Element in a Stream</a></p></li><li><p><a href="https://leetcode.com/problems/relative-ranks/">Relative Ranks</a></p></li><li><p><a href="https://leetcode.com/problems/k-closest-points-to-origin/">K Closest Points to Origin</a></p></li><li><p><a href="https://leetcode.com/problems/top-k-frequent-elements/">Top K Frequent Elements</a></p></li><li><p><a href="https://leetcode.com/problems/merge-k-sorted-lists/">Merge k Sorted Lists</a></p></li></ol><h3><strong>11/ Graphs</strong></h3><ol><li><p><a href="https://leetcode.com/problems/find-center-of-star-graph/">Find Center of Star Graph</a></p></li><li><p><a href="https://leetcode.com/problems/find-the-town-judge/">Find the Town Judge</a></p></li><li><p><a href="https://leetcode.com/problems/find-if-path-exists-in-graph/">Find if Path Exists in Graph</a></p></li><li><p><a href="https://leetcode.com/problems/clone-graph/">Clone Graph</a></p></li><li><p><a href="https://leetcode.com/problems/course-schedule/">Course Schedule</a></p></li><li><p><a href="https://leetcode.com/problems/longest-increasing-path-in-a-matrix/">Longest Increasing Path in a Matrix</a></p></li></ol><h3><strong>12/ Intervals</strong></h3><ol><li><p><a href="https://leetcode.com/problems/interval-list-intersections/">Interval List Intersections</a></p></li><li><p><a href="https://leetcode.com/problems/merge-intervals/">Merge Intervals</a></p></li><li><p><a href="https://leetcode.com/problems/insert-interval/">Insert Interval</a></p></li></ol><h3><strong>13/ Greedy &amp; Dynamic Programming</strong></h3><ol><li><p><a href="https://leetcode.com/problems/best-time-to-buy-and-sell-stock/">Best Time to Buy and Sell Stock</a></p></li><li><p><a href="https://leetcode.com/problems/maximum-subarray/">Maximum Subarray</a></p></li><li><p><a href="https://leetcode.com/problems/trapping-rain-water/">Trapping Rain Water</a></p></li></ol><h3><strong>14/ Prefix Sum</strong></h3><ol><li><p><a href="https://leetcode.com/problems/subarray-sum-equals-k/">Subarray Sum Equals K</a></p></li><li><p><a href="https://leetcode.com/problems/continuous-subarray-sum/">Continuous Subarray Sum</a></p></li><li><p><a href="https://leetcode.com/problems/product-of-array-except-self/">Product of Array Except Self</a></p></li></ol><h3><strong>15/ Miscellaneous</strong></h3><ol><li><p><a href="https://leetcode.com/problems/powx-n/">Pow(x, n)</a></p></li><li><p><a href="https://leetcode.com/problems/making-a-large-island/">Making A Large Island</a></p></li></ol><div><hr></div><p>If you&#8217;re preparing for FAANG in 2026, don&#8217;t forget to save this post.</p><p>Also, here&#8217;s a platform which you can use to practice for your interviews:</p><p><strong>&#128279; Interview Roadmap Platform:</strong> <a href="https://interview-roadmap.vercel.app/">Link</a></p><p>Best of luck!</p>]]></content:encoded></item><item><title><![CDATA[Find Center of Graph]]></title><description><![CDATA[Tesla Interview Question]]></description><link>https://singhz.substack.com/p/find-center-of-graph-40c</link><guid isPermaLink="false">https://singhz.substack.com/p/find-center-of-graph-40c</guid><dc:creator><![CDATA[Harpreet Singh]]></dc:creator><pubDate>Mon, 27 Apr 2026 14:01:34 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!R0tU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51f60f07-b249-4c60-855c-e5ce07108579_1280x720.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!R0tU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51f60f07-b249-4c60-855c-e5ce07108579_1280x720.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!R0tU!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51f60f07-b249-4c60-855c-e5ce07108579_1280x720.png 424w, /__u/substackcdn.com/image/fetch/$s_!R0tU!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51f60f07-b249-4c60-855c-e5ce07108579_1280x720.png 848w, /__u/substackcdn.com/image/fetch/$s_!R0tU!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51f60f07-b249-4c60-855c-e5ce07108579_1280x720.png 1272w, /__u/substackcdn.com/image/fetch/$s_!R0tU!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51f60f07-b249-4c60-855c-e5ce07108579_1280x720.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!R0tU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51f60f07-b249-4c60-855c-e5ce07108579_1280x720.png" width="1280" height="720" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/51f60f07-b249-4c60-855c-e5ce07108579_1280x720.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:720,&quot;width&quot;:1280,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:308505,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/186242235?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51f60f07-b249-4c60-855c-e5ce07108579_1280x720.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!R0tU!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51f60f07-b249-4c60-855c-e5ce07108579_1280x720.png 424w, /__u/substackcdn.com/image/fetch/$s_!R0tU!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51f60f07-b249-4c60-855c-e5ce07108579_1280x720.png 848w, /__u/substackcdn.com/image/fetch/$s_!R0tU!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51f60f07-b249-4c60-855c-e5ce07108579_1280x720.png 1272w, /__u/substackcdn.com/image/fetch/$s_!R0tU!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51f60f07-b249-4c60-855c-e5ce07108579_1280x720.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3><strong>Problem Overview</strong></h3><blockquote><p><strong>Difficulty: </strong>Easy<br><strong>LeetCode Pattern:</strong> Graph</p></blockquote><p>You are given a 2D integer array <strong>edges</strong> where each <strong>edges[i] = [ui, vi]</strong> indicates that there is an edge between the nodes <strong>ui</strong> and <strong>vi</strong>. Return the center of the given graph.</p><p><strong>Example</strong></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!IHCl!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc49dae48-f25d-454b-bc7a-70d620b7a932_1434x734.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!IHCl!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc49dae48-f25d-454b-bc7a-70d620b7a932_1434x734.png 424w, /__u/substackcdn.com/image/fetch/$s_!IHCl!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc49dae48-f25d-454b-bc7a-70d620b7a932_1434x734.png 848w, /__u/substackcdn.com/image/fetch/$s_!IHCl!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc49dae48-f25d-454b-bc7a-70d620b7a932_1434x734.png 1272w, /__u/substackcdn.com/image/fetch/$s_!IHCl!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc49dae48-f25d-454b-bc7a-70d620b7a932_1434x734.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!IHCl!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc49dae48-f25d-454b-bc7a-70d620b7a932_1434x734.png" width="1434" height="734" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c49dae48-f25d-454b-bc7a-70d620b7a932_1434x734.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:734,&quot;width&quot;:1434,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:90229,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/186242235?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc49dae48-f25d-454b-bc7a-70d620b7a932_1434x734.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!IHCl!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc49dae48-f25d-454b-bc7a-70d620b7a932_1434x734.png 424w, /__u/substackcdn.com/image/fetch/$s_!IHCl!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc49dae48-f25d-454b-bc7a-70d620b7a932_1434x734.png 848w, /__u/substackcdn.com/image/fetch/$s_!IHCl!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc49dae48-f25d-454b-bc7a-70d620b7a932_1434x734.png 1272w, /__u/substackcdn.com/image/fetch/$s_!IHCl!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc49dae48-f25d-454b-bc7a-70d620b7a932_1434x734.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><pre><code><strong>Input:</strong>
&#183; edges = [[1,2],[2,3],[4,2]]
<strong>Output:</strong> 
&#183; 2
<strong>Reason:
</strong>&#183; 2 is the center of this graph</code></pre><div><hr></div><h3><strong>Step 1: Clarify Requirements</strong></h3><ul><li><p><strong>Can edges be empty?</strong></p><ul><li><p>No.</p></li></ul></li><li><p><strong>Can there be multiple centers?</strong></p><ul><li><p>No.</p></li></ul></li><li><p><strong>Is graph guaranteed to have a center?</strong></p><ul><li><p>Yes.</p></li></ul></li></ul><div><hr></div><h3><strong>Step 2: Discuss Approaches</strong></h3><p><strong>1/ Use a HashMap (Suboptimal &#9888;&#65039;)</strong></p><ul><li><p><strong>Logic:</strong></p><ul><li><p>Count # of edges for each node</p></li><li><p>Store these in a HashMap</p></li><li><p>Return node with max degree</p></li></ul></li><li><p><strong>Big O:</strong></p><ul><li><p>Time Complexity: O(n)</p></li><li><p>Space Complexity: O(n)</p></li></ul></li></ul><div><hr></div><p><strong>2/ Check Two Edges (Optimal &#9989;)</strong></p><ul><li><p><strong>Logic:</strong></p><ul><li><p>We only need to check first 2 edges</p></li><li><p>Node common to both is the center</p></li></ul></li><li><p><strong>Big O:</strong></p><ul><li><p>Time Complexity: O(1)</p></li><li><p>Space Complexity: O(1)</p></li></ul></li></ul><div><hr></div><h3><strong>Step 3: Write Code (Optimal)</strong></h3><h4><strong>Python</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!VT9T!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82051d8d-cd61-40eb-80a2-509804743c46_822x460.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!VT9T!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82051d8d-cd61-40eb-80a2-509804743c46_822x460.png 424w, /__u/substackcdn.com/image/fetch/$s_!VT9T!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82051d8d-cd61-40eb-80a2-509804743c46_822x460.png 848w, /__u/substackcdn.com/image/fetch/$s_!VT9T!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82051d8d-cd61-40eb-80a2-509804743c46_822x460.png 1272w, /__u/substackcdn.com/image/fetch/$s_!VT9T!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82051d8d-cd61-40eb-80a2-509804743c46_822x460.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!VT9T!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82051d8d-cd61-40eb-80a2-509804743c46_822x460.png" width="822" height="460" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/82051d8d-cd61-40eb-80a2-509804743c46_822x460.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:460,&quot;width&quot;:822,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:56007,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/170041217?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82051d8d-cd61-40eb-80a2-509804743c46_822x460.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!VT9T!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82051d8d-cd61-40eb-80a2-509804743c46_822x460.png 424w, /__u/substackcdn.com/image/fetch/$s_!VT9T!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82051d8d-cd61-40eb-80a2-509804743c46_822x460.png 848w, /__u/substackcdn.com/image/fetch/$s_!VT9T!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82051d8d-cd61-40eb-80a2-509804743c46_822x460.png 1272w, /__u/substackcdn.com/image/fetch/$s_!VT9T!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82051d8d-cd61-40eb-80a2-509804743c46_822x460.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4><strong>Java</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!vfHJ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f0e2414-015c-42bd-8865-76fd351a2be8_830x444.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!vfHJ!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f0e2414-015c-42bd-8865-76fd351a2be8_830x444.png 424w, /__u/substackcdn.com/image/fetch/$s_!vfHJ!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f0e2414-015c-42bd-8865-76fd351a2be8_830x444.png 848w, /__u/substackcdn.com/image/fetch/$s_!vfHJ!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f0e2414-015c-42bd-8865-76fd351a2be8_830x444.png 1272w, /__u/substackcdn.com/image/fetch/$s_!vfHJ!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f0e2414-015c-42bd-8865-76fd351a2be8_830x444.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!vfHJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f0e2414-015c-42bd-8865-76fd351a2be8_830x444.png" width="830" height="444" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1f0e2414-015c-42bd-8865-76fd351a2be8_830x444.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:444,&quot;width&quot;:830,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:69866,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/170828769?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f0e2414-015c-42bd-8865-76fd351a2be8_830x444.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!vfHJ!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f0e2414-015c-42bd-8865-76fd351a2be8_830x444.png 424w, /__u/substackcdn.com/image/fetch/$s_!vfHJ!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f0e2414-015c-42bd-8865-76fd351a2be8_830x444.png 848w, /__u/substackcdn.com/image/fetch/$s_!vfHJ!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f0e2414-015c-42bd-8865-76fd351a2be8_830x444.png 1272w, /__u/substackcdn.com/image/fetch/$s_!vfHJ!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f0e2414-015c-42bd-8865-76fd351a2be8_830x444.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4><strong>C++</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!iDpR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb253c144-3d29-41a9-9e4b-e65732e74b29_798x458.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!iDpR!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb253c144-3d29-41a9-9e4b-e65732e74b29_798x458.png 424w, /__u/substackcdn.com/image/fetch/$s_!iDpR!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb253c144-3d29-41a9-9e4b-e65732e74b29_798x458.png 848w, /__u/substackcdn.com/image/fetch/$s_!iDpR!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb253c144-3d29-41a9-9e4b-e65732e74b29_798x458.png 1272w, /__u/substackcdn.com/image/fetch/$s_!iDpR!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb253c144-3d29-41a9-9e4b-e65732e74b29_798x458.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!iDpR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb253c144-3d29-41a9-9e4b-e65732e74b29_798x458.png" width="798" height="458" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b253c144-3d29-41a9-9e4b-e65732e74b29_798x458.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:458,&quot;width&quot;:798,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:69460,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/170828769?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb253c144-3d29-41a9-9e4b-e65732e74b29_798x458.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!iDpR!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb253c144-3d29-41a9-9e4b-e65732e74b29_798x458.png 424w, /__u/substackcdn.com/image/fetch/$s_!iDpR!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb253c144-3d29-41a9-9e4b-e65732e74b29_798x458.png 848w, /__u/substackcdn.com/image/fetch/$s_!iDpR!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb253c144-3d29-41a9-9e4b-e65732e74b29_798x458.png 1272w, /__u/substackcdn.com/image/fetch/$s_!iDpR!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb253c144-3d29-41a9-9e4b-e65732e74b29_798x458.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h3><strong>Step 4: Answer Follow-Ups</strong></h3><ul><li><p><strong>What if no guaranteed center?</strong></p><ul><li><p>If no center, return -1.</p></li></ul></li><li><p><strong>What if graph is directed?</strong></p><ul><li><p>Center = node with all incoming edges.</p></li></ul></li></ul><div><hr></div><h3><strong>&#128104;&#8205;&#128187; Need Interview Practice?</strong></h3><ul><li><p>Book a <strong>1:1</strong> with me</p></li><li><p><strong>Book Here: </strong><a href="https://linktr.ee/singhz.official">Booking URL</a></p></li></ul>]]></content:encoded></item><item><title><![CDATA[Max Area of Island]]></title><description><![CDATA[NVIDIA Interview Question]]></description><link>https://singhz.substack.com/p/max-area-of-island</link><guid isPermaLink="false">https://singhz.substack.com/p/max-area-of-island</guid><pubDate>Mon, 20 Apr 2026 14:02:27 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!eg0X!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd18e0ee0-b2ce-40df-a0c6-00e2ff086b15_1280x720.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!eg0X!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd18e0ee0-b2ce-40df-a0c6-00e2ff086b15_1280x720.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!eg0X!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd18e0ee0-b2ce-40df-a0c6-00e2ff086b15_1280x720.png 424w, /__u/substackcdn.com/image/fetch/$s_!eg0X!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd18e0ee0-b2ce-40df-a0c6-00e2ff086b15_1280x720.png 848w, /__u/substackcdn.com/image/fetch/$s_!eg0X!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd18e0ee0-b2ce-40df-a0c6-00e2ff086b15_1280x720.png 1272w, /__u/substackcdn.com/image/fetch/$s_!eg0X!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd18e0ee0-b2ce-40df-a0c6-00e2ff086b15_1280x720.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!eg0X!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd18e0ee0-b2ce-40df-a0c6-00e2ff086b15_1280x720.png" width="1280" height="720" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d18e0ee0-b2ce-40df-a0c6-00e2ff086b15_1280x720.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:720,&quot;width&quot;:1280,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:311119,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/186240690?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd18e0ee0-b2ce-40df-a0c6-00e2ff086b15_1280x720.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!eg0X!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd18e0ee0-b2ce-40df-a0c6-00e2ff086b15_1280x720.png 424w, /__u/substackcdn.com/image/fetch/$s_!eg0X!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd18e0ee0-b2ce-40df-a0c6-00e2ff086b15_1280x720.png 848w, /__u/substackcdn.com/image/fetch/$s_!eg0X!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd18e0ee0-b2ce-40df-a0c6-00e2ff086b15_1280x720.png 1272w, /__u/substackcdn.com/image/fetch/$s_!eg0X!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd18e0ee0-b2ce-40df-a0c6-00e2ff086b15_1280x720.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3><strong>Problem Overview</strong></h3><blockquote><p><strong>Difficulty: </strong>Medium<br><strong>LeetCode Pattern:</strong> DFS</p></blockquote><p>Given an <strong>m x n</strong> binary grid which represents a map of <strong>&#8216;1&#8217;s (land) and &#8216;0&#8217;s (water)</strong>, return the <strong>maximum area of an island</strong> in grid. If there is no island, return 0.</p><p><em>Note: An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. Assume all four edges of the grid are all surrounded by water.</em></p><pre><code><strong>Input:</strong> grid = [
  ["1","1","0","0","0"],
  ["1","1","0","0","0"],
  ["0","0","1","0","0"],
  ["0","0","0","1","1"]
]

<strong>Output:</strong>
&#183; 4

<strong>Reason:</strong>
&#183; This grid has three islands
&#183; Area of first island = 4 &#8594; maximum
&#183; Area of second island = 1
&#183; Area of third island = 2</code></pre><div><hr></div><h3><strong>Step 1: Clarify Requirements</strong></h3><ul><li><p><strong>Can the grid be empty?</strong></p><ul><li><p>Yes, it can be empty. </p></li><li><p>Return 0 in that case.</p></li></ul></li><li><p><strong>Will it only contains 0&#8217;s and 1s?</strong></p><ul><li><p>Yes, you can assume that.</p></li></ul></li><li><p><strong>Will 0&#8217;s and 1&#8217;s be integers or strings?</strong></p><ul><li><p>They will be given as strings.</p></li></ul></li></ul><div><hr></div><h3><strong>Step 2: Discuss Approaches</strong></h3><p><strong>1/ Use DFS (Optimal &#9989;)</strong></p><ul><li><p><strong>Logic:</strong></p><ul><li><p>Iterate through every cell in the grid</p></li><li><p>When a '1' (land) is found:</p><ul><li><p>Run DFS starting here</p></li><li><p>Mark visited cells as '0'</p></li><li><p>Track area for this island</p></li><li><p>If current area is larger:</p><ul><li><p>Update maxArea</p></li></ul></li></ul></li><li><p>At the end, return the maxArea</p></li></ul></li><li><p><strong>Big O:</strong></p><ul><li><p>Time Complexity: O(m&#183;n)</p></li><li><p>Space Complexity: O(m&#183;n)</p></li></ul></li></ul><div><hr></div><p><strong>2/ Use BFS (Alternative &#9989;)</strong></p><ul><li><p><strong>Logic:</strong></p><ul><li><p>Iterate through the grid</p></li><li><p>When a '1' is found:</p><ul><li><p>Push cell into a queue</p></li><li><p>While queue is not empty:</p><ul><li><p>Pop cell, mark as '0'</p></li><li><p>Push all valid neighbors</p></li><li><p>Count cells to get area</p></li></ul></li><li><p>If current area is larger:</p><ul><li><p>Update maxArea</p></li></ul></li></ul></li><li><p>At the end, return the maxArea</p></li></ul></li><li><p><strong>Big O:</strong></p><ul><li><p>Time Complexity: O(m&#183;n)</p></li><li><p>Space Complexity: O(m&#183;n)</p></li></ul></li></ul><div><hr></div><h3><strong>Step 3: Write Code (Optimal)</strong></h3><h4><strong>Python</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!kIIy!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0eeab135-fc9a-48bd-ba36-f1f6f0579b62_1020x1126.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!kIIy!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0eeab135-fc9a-48bd-ba36-f1f6f0579b62_1020x1126.png 424w, /__u/substackcdn.com/image/fetch/$s_!kIIy!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0eeab135-fc9a-48bd-ba36-f1f6f0579b62_1020x1126.png 848w, /__u/substackcdn.com/image/fetch/$s_!kIIy!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0eeab135-fc9a-48bd-ba36-f1f6f0579b62_1020x1126.png 1272w, /__u/substackcdn.com/image/fetch/$s_!kIIy!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0eeab135-fc9a-48bd-ba36-f1f6f0579b62_1020x1126.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!kIIy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0eeab135-fc9a-48bd-ba36-f1f6f0579b62_1020x1126.png" width="1020" height="1126" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0eeab135-fc9a-48bd-ba36-f1f6f0579b62_1020x1126.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1126,&quot;width&quot;:1020,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:186355,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/186240690?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0eeab135-fc9a-48bd-ba36-f1f6f0579b62_1020x1126.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!kIIy!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0eeab135-fc9a-48bd-ba36-f1f6f0579b62_1020x1126.png 424w, /__u/substackcdn.com/image/fetch/$s_!kIIy!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0eeab135-fc9a-48bd-ba36-f1f6f0579b62_1020x1126.png 848w, /__u/substackcdn.com/image/fetch/$s_!kIIy!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0eeab135-fc9a-48bd-ba36-f1f6f0579b62_1020x1126.png 1272w, /__u/substackcdn.com/image/fetch/$s_!kIIy!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0eeab135-fc9a-48bd-ba36-f1f6f0579b62_1020x1126.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4><strong>Java</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Vfny!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F463cb45b-9635-4c16-897a-3e7df6f02617_1082x1088.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Vfny!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F463cb45b-9635-4c16-897a-3e7df6f02617_1082x1088.png 424w, /__u/substackcdn.com/image/fetch/$s_!Vfny!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F463cb45b-9635-4c16-897a-3e7df6f02617_1082x1088.png 848w, /__u/substackcdn.com/image/fetch/$s_!Vfny!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F463cb45b-9635-4c16-897a-3e7df6f02617_1082x1088.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Vfny!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F463cb45b-9635-4c16-897a-3e7df6f02617_1082x1088.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Vfny!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F463cb45b-9635-4c16-897a-3e7df6f02617_1082x1088.png" width="1082" height="1088" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/463cb45b-9635-4c16-897a-3e7df6f02617_1082x1088.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1088,&quot;width&quot;:1082,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:189881,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/186240690?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F463cb45b-9635-4c16-897a-3e7df6f02617_1082x1088.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!Vfny!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F463cb45b-9635-4c16-897a-3e7df6f02617_1082x1088.png 424w, /__u/substackcdn.com/image/fetch/$s_!Vfny!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F463cb45b-9635-4c16-897a-3e7df6f02617_1082x1088.png 848w, /__u/substackcdn.com/image/fetch/$s_!Vfny!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F463cb45b-9635-4c16-897a-3e7df6f02617_1082x1088.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Vfny!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F463cb45b-9635-4c16-897a-3e7df6f02617_1082x1088.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4><strong>C++</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!AQOd!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcb9c897-185f-4c93-9378-2db5d005818c_1026x1086.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!AQOd!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcb9c897-185f-4c93-9378-2db5d005818c_1026x1086.png 424w, /__u/substackcdn.com/image/fetch/$s_!AQOd!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcb9c897-185f-4c93-9378-2db5d005818c_1026x1086.png 848w, /__u/substackcdn.com/image/fetch/$s_!AQOd!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcb9c897-185f-4c93-9378-2db5d005818c_1026x1086.png 1272w, /__u/substackcdn.com/image/fetch/$s_!AQOd!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcb9c897-185f-4c93-9378-2db5d005818c_1026x1086.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!AQOd!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcb9c897-185f-4c93-9378-2db5d005818c_1026x1086.png" width="1026" height="1086" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bcb9c897-185f-4c93-9378-2db5d005818c_1026x1086.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1086,&quot;width&quot;:1026,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:192433,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/186240690?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcb9c897-185f-4c93-9378-2db5d005818c_1026x1086.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!AQOd!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcb9c897-185f-4c93-9378-2db5d005818c_1026x1086.png 424w, /__u/substackcdn.com/image/fetch/$s_!AQOd!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcb9c897-185f-4c93-9378-2db5d005818c_1026x1086.png 848w, /__u/substackcdn.com/image/fetch/$s_!AQOd!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcb9c897-185f-4c93-9378-2db5d005818c_1026x1086.png 1272w, /__u/substackcdn.com/image/fetch/$s_!AQOd!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcb9c897-185f-4c93-9378-2db5d005818c_1026x1086.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h3><strong>Step 4: Answer Follow-Ups</strong></h3><ul><li><p><strong>What if the grid is empty?</strong></p><ul><li><p>Return 0.</p></li><li><p>No cells &#8594; no islands &#8594; no area.</p></li></ul></li><li><p><strong>What if we allow diagonal connections?</strong></p><ul><li><p>Add 4 more directions in DFS.</p></li><li><p>Total neighbors = 8 instead of 4.</p></li></ul></li><li><p><strong>What if the grid is very large?</strong></p><ul><li><p>DFS may cause stack overflow.</p></li><li><p>Use BFS or iterative DFS with stack.</p></li></ul></li></ul>]]></content:encoded></item><item><title><![CDATA[Diameter of Binary Tree]]></title><description><![CDATA[Accenture Interview Question]]></description><link>https://singhz.substack.com/p/diameter-of-binary-tree</link><guid isPermaLink="false">https://singhz.substack.com/p/diameter-of-binary-tree</guid><dc:creator><![CDATA[Harpreet Singh]]></dc:creator><pubDate>Mon, 13 Apr 2026 14:01:39 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!Yx3u!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef49de8d-42ac-44c3-b032-306a32a45419_1280x720.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Yx3u!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef49de8d-42ac-44c3-b032-306a32a45419_1280x720.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Yx3u!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef49de8d-42ac-44c3-b032-306a32a45419_1280x720.png 424w, /__u/substackcdn.com/image/fetch/$s_!Yx3u!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef49de8d-42ac-44c3-b032-306a32a45419_1280x720.png 848w, /__u/substackcdn.com/image/fetch/$s_!Yx3u!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef49de8d-42ac-44c3-b032-306a32a45419_1280x720.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Yx3u!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef49de8d-42ac-44c3-b032-306a32a45419_1280x720.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Yx3u!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef49de8d-42ac-44c3-b032-306a32a45419_1280x720.png" width="1280" height="720" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ef49de8d-42ac-44c3-b032-306a32a45419_1280x720.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:720,&quot;width&quot;:1280,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:313346,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/186238530?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef49de8d-42ac-44c3-b032-306a32a45419_1280x720.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!Yx3u!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef49de8d-42ac-44c3-b032-306a32a45419_1280x720.png 424w, /__u/substackcdn.com/image/fetch/$s_!Yx3u!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef49de8d-42ac-44c3-b032-306a32a45419_1280x720.png 848w, /__u/substackcdn.com/image/fetch/$s_!Yx3u!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef49de8d-42ac-44c3-b032-306a32a45419_1280x720.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Yx3u!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef49de8d-42ac-44c3-b032-306a32a45419_1280x720.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3><strong>Problem Overview</strong></h3><blockquote><p><strong>Difficulty: </strong>Easy<br><strong>LeetCode Pattern:</strong> DFS</p></blockquote><p>Given the root of a binary tree, return the length of the diameter of the tree.</p><p><em>Note: </em></p><ul><li><p><em>The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. </em></p></li><li><p><em>The length of a path between two nodes is represented by the number of edges between them.</em></p></li></ul><pre><code><strong>Input:</strong>
    1
   / \
  2   3
 / \
4   5

<strong>Output:</strong> 3

<strong>Reason:</strong> 3 is the length of the path [4,2,1,3] or [5,2,1,3].</code></pre><div><hr></div><h3><strong>Step 1: Clarify Requirements</strong></h3><ul><li><p><strong>Can the tree be empty?</strong></p><ul><li><p>Yes, it can be empty. </p></li><li><p>Diameter = 0 in that case.</p></li></ul></li><li><p><strong>What if tree has just root node?</strong></p><ul><li><p>Diameter = 0 in that case also.</p></li></ul></li><li><p><strong>Do we need the path itself?</strong></p><ul><li><p>No, just the length of diameter.</p></li></ul></li></ul><div><hr></div><h3><strong>Step 2: Discuss Approaches</strong></h3><p><strong>1/ Check All Diameters (Brute Force &#9888;&#65039;)</strong></p><ul><li><p><strong>Logic:</strong></p><ul><li><p>For every node in the tree:</p><ul><li><p>Get left subtree height</p></li><li><p>Get right subtree height</p></li><li><p>Diameter = left + right</p></li></ul></li><li><p>Keep track of max diameter seen</p></li></ul></li><li><p><strong>Big O:</strong></p><ul><li><p>Time Complexity: O(n&#178;)</p></li><li><p>Space Complexity: O(h)</p></li></ul></li></ul><div><hr></div><p><strong>2/ Use Post-Order DFS (Optimal &#9989;)</strong></p><ul><li><p><strong>Logic:</strong></p><ul><li><p>Use post-order DFS:</p><ul><li><p>left &#8594; right &#8594; node</p></li></ul></li><li><p>For each node:</p><ul><li><p>Compute left subtree height</p></li><li><p>Compute right subtree height</p></li><li><p>max = max(curr, left + right)</p></li></ul></li><li><p>Return height = 1 + max(left, right)</p></li><li><p>At the end, return maxDiameter</p></li></ul></li><li><p><strong>Big O:</strong></p><ul><li><p>Time Complexity: O(n)</p></li><li><p>Space Complexity: O(h)</p></li></ul></li></ul><div><hr></div><h3><strong>Step 3: Write Code (Optimal)</strong></h3><h4><strong>Python</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!t0Ms!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61efa83b-a329-485e-9768-1b8cc701e3c7_1036x790.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!t0Ms!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61efa83b-a329-485e-9768-1b8cc701e3c7_1036x790.png 424w, /__u/substackcdn.com/image/fetch/$s_!t0Ms!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61efa83b-a329-485e-9768-1b8cc701e3c7_1036x790.png 848w, /__u/substackcdn.com/image/fetch/$s_!t0Ms!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61efa83b-a329-485e-9768-1b8cc701e3c7_1036x790.png 1272w, /__u/substackcdn.com/image/fetch/$s_!t0Ms!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61efa83b-a329-485e-9768-1b8cc701e3c7_1036x790.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!t0Ms!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61efa83b-a329-485e-9768-1b8cc701e3c7_1036x790.png" width="1036" height="790" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/61efa83b-a329-485e-9768-1b8cc701e3c7_1036x790.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:790,&quot;width&quot;:1036,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:157230,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/186238530?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61efa83b-a329-485e-9768-1b8cc701e3c7_1036x790.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!t0Ms!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61efa83b-a329-485e-9768-1b8cc701e3c7_1036x790.png 424w, /__u/substackcdn.com/image/fetch/$s_!t0Ms!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61efa83b-a329-485e-9768-1b8cc701e3c7_1036x790.png 848w, /__u/substackcdn.com/image/fetch/$s_!t0Ms!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61efa83b-a329-485e-9768-1b8cc701e3c7_1036x790.png 1272w, /__u/substackcdn.com/image/fetch/$s_!t0Ms!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61efa83b-a329-485e-9768-1b8cc701e3c7_1036x790.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4><strong>Java</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!AEEA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F449e85e8-71b2-4a15-a425-7b41271c687f_1102x864.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!AEEA!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F449e85e8-71b2-4a15-a425-7b41271c687f_1102x864.png 424w, /__u/substackcdn.com/image/fetch/$s_!AEEA!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F449e85e8-71b2-4a15-a425-7b41271c687f_1102x864.png 848w, /__u/substackcdn.com/image/fetch/$s_!AEEA!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F449e85e8-71b2-4a15-a425-7b41271c687f_1102x864.png 1272w, /__u/substackcdn.com/image/fetch/$s_!AEEA!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F449e85e8-71b2-4a15-a425-7b41271c687f_1102x864.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!AEEA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F449e85e8-71b2-4a15-a425-7b41271c687f_1102x864.png" width="1102" height="864" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/449e85e8-71b2-4a15-a425-7b41271c687f_1102x864.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:864,&quot;width&quot;:1102,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:150737,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/186238530?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F449e85e8-71b2-4a15-a425-7b41271c687f_1102x864.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!AEEA!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F449e85e8-71b2-4a15-a425-7b41271c687f_1102x864.png 424w, /__u/substackcdn.com/image/fetch/$s_!AEEA!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F449e85e8-71b2-4a15-a425-7b41271c687f_1102x864.png 848w, /__u/substackcdn.com/image/fetch/$s_!AEEA!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F449e85e8-71b2-4a15-a425-7b41271c687f_1102x864.png 1272w, /__u/substackcdn.com/image/fetch/$s_!AEEA!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F449e85e8-71b2-4a15-a425-7b41271c687f_1102x864.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4><strong>C++</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!8YE7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6874c56-7c49-490d-b549-d4a2178dd8e8_1054x866.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!8YE7!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6874c56-7c49-490d-b549-d4a2178dd8e8_1054x866.png 424w, /__u/substackcdn.com/image/fetch/$s_!8YE7!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6874c56-7c49-490d-b549-d4a2178dd8e8_1054x866.png 848w, /__u/substackcdn.com/image/fetch/$s_!8YE7!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6874c56-7c49-490d-b549-d4a2178dd8e8_1054x866.png 1272w, /__u/substackcdn.com/image/fetch/$s_!8YE7!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6874c56-7c49-490d-b549-d4a2178dd8e8_1054x866.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!8YE7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6874c56-7c49-490d-b549-d4a2178dd8e8_1054x866.png" width="1054" height="866" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a6874c56-7c49-490d-b549-d4a2178dd8e8_1054x866.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:866,&quot;width&quot;:1054,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:145734,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/186238530?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6874c56-7c49-490d-b549-d4a2178dd8e8_1054x866.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!8YE7!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6874c56-7c49-490d-b549-d4a2178dd8e8_1054x866.png 424w, /__u/substackcdn.com/image/fetch/$s_!8YE7!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6874c56-7c49-490d-b549-d4a2178dd8e8_1054x866.png 848w, /__u/substackcdn.com/image/fetch/$s_!8YE7!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6874c56-7c49-490d-b549-d4a2178dd8e8_1054x866.png 1272w, /__u/substackcdn.com/image/fetch/$s_!8YE7!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6874c56-7c49-490d-b549-d4a2178dd8e8_1054x866.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h3><strong>Step 4: Answer Follow-Ups</strong></h3><ul><li><p><strong>What if tree is very large?</strong></p><ul><li><p>Recursion may hit stack overflow.</p></li><li><p>Use iterative DFS instead.</p></li></ul></li><li><p><strong>What if we need to return path?</strong></p><ul><li><p>Track nodes instead of just lengths.</p></li><li><p>During DFS, for each node:</p><ul><li><p>Get left path</p></li><li><p>Get right path</p></li><li><p>curr_path = left + [node] + right</p></li><li><p>If curr_path size &gt; max_path:</p><ul><li><p>update max_path</p></li></ul></li></ul></li><li><p>Return max_path at the end.</p></li></ul></li></ul>]]></content:encoded></item><item><title><![CDATA[Number of Islands]]></title><description><![CDATA[IBM Interview Question]]></description><link>https://singhz.substack.com/p/number-of-islands</link><guid isPermaLink="false">https://singhz.substack.com/p/number-of-islands</guid><dc:creator><![CDATA[Harpreet Singh]]></dc:creator><pubDate>Mon, 06 Apr 2026 14:01:46 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!RLqp!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd43d8b48-c254-45ae-a384-a8db038465ab_1280x720.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!RLqp!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd43d8b48-c254-45ae-a384-a8db038465ab_1280x720.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!RLqp!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd43d8b48-c254-45ae-a384-a8db038465ab_1280x720.png 424w, /__u/substackcdn.com/image/fetch/$s_!RLqp!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd43d8b48-c254-45ae-a384-a8db038465ab_1280x720.png 848w, /__u/substackcdn.com/image/fetch/$s_!RLqp!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd43d8b48-c254-45ae-a384-a8db038465ab_1280x720.png 1272w, /__u/substackcdn.com/image/fetch/$s_!RLqp!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd43d8b48-c254-45ae-a384-a8db038465ab_1280x720.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!RLqp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd43d8b48-c254-45ae-a384-a8db038465ab_1280x720.png" width="1280" height="720" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d43d8b48-c254-45ae-a384-a8db038465ab_1280x720.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:720,&quot;width&quot;:1280,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:310656,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/186236709?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd43d8b48-c254-45ae-a384-a8db038465ab_1280x720.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!RLqp!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd43d8b48-c254-45ae-a384-a8db038465ab_1280x720.png 424w, /__u/substackcdn.com/image/fetch/$s_!RLqp!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd43d8b48-c254-45ae-a384-a8db038465ab_1280x720.png 848w, /__u/substackcdn.com/image/fetch/$s_!RLqp!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd43d8b48-c254-45ae-a384-a8db038465ab_1280x720.png 1272w, /__u/substackcdn.com/image/fetch/$s_!RLqp!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd43d8b48-c254-45ae-a384-a8db038465ab_1280x720.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3><strong>Problem Overview</strong></h3><blockquote><p><strong>Difficulty: </strong>Medium<br><strong>LeetCode Pattern:</strong> DFS</p></blockquote><p>Given an <strong>m x n</strong> binary grid which represents a map of <strong>&#8216;1&#8217;s (land) and &#8216;0&#8217;s (water)</strong>, return the number of islands.</p><p><em>Note: An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. Assume all four edges of the grid are all surrounded by water.</em></p><p><strong>Example</strong></p><pre><code><strong>Input:</strong> grid = [
  ["1","1","0","0","0"],
  ["1","1","0","0","0"],
  ["0","0","1","0","0"],
  ["0","0","0","1","1"]
]

<strong>Output:</strong>
&#183; 3</code></pre><div><hr></div><h3><strong>Step 1: Clarify Requirements</strong></h3><ul><li><p><strong>Can the grid be empty?</strong></p><ul><li><p>Yes, it can be empty. </p></li><li><p>Return 0 in that case.</p></li></ul></li><li><p><strong>Will it only contains 0&#8217;s and 1s?</strong></p><ul><li><p>Yes, you can assume that.</p></li></ul></li><li><p><strong>Will 0&#8217;s and 1&#8217;s be integers or strings?</strong></p><ul><li><p>They will be given as strings.</p></li></ul></li></ul><div><hr></div><h3><strong>Step 2: Discuss Approaches</strong></h3><p><strong>1/ Use DFS (Optimal &#9989;)</strong></p><ul><li><p><strong>Logic:</strong></p><ul><li><p>Iterate through every cell in the grid</p></li><li><p>When a '1' (land) is found:</p><ul><li><p>Increment island count</p></li><li><p>Run DFS from this point</p></li></ul></li><li><p>During DFS:</p><ul><li><p>Mark current cell as '0'</p></li><li><p>Recurse up, down, left, right</p></li></ul></li><li><p>At the end, return island count</p></li></ul></li><li><p><strong>Big O:</strong></p><ul><li><p>Time Complexity: O(m&#183;n)</p></li><li><p>Space Complexity: O(m&#183;n)</p></li></ul></li></ul><div><hr></div><p><strong>2/ Use BFS (Alternative &#9989;)</strong></p><ul><li><p><strong>Logic:</strong></p><ul><li><p>Iterate through grid</p></li><li><p>When a '1' is found:</p><ul><li><p>Increment island count</p></li><li><p>Push cell into queue</p></li></ul></li><li><p>While queue not empty:</p><ul><li><p>Pop cell</p></li><li><p>Mark it as visited ('0')</p></li><li><p>Push all valid land neighbors</p></li></ul></li></ul></li><li><p><strong>Big O:</strong></p><ul><li><p>Time Complexity: O(m&#183;n)</p></li><li><p>Space Complexity: O(m&#183;n)</p></li></ul></li></ul><div><hr></div><h3><strong>Step 3: Write Code (Optimal)</strong></h3><h4><strong>Python</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!NtIA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92d9a15-cd0d-40cf-a61d-680352cf6c12_1022x1392.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!NtIA!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92d9a15-cd0d-40cf-a61d-680352cf6c12_1022x1392.png 424w, /__u/substackcdn.com/image/fetch/$s_!NtIA!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92d9a15-cd0d-40cf-a61d-680352cf6c12_1022x1392.png 848w, /__u/substackcdn.com/image/fetch/$s_!NtIA!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92d9a15-cd0d-40cf-a61d-680352cf6c12_1022x1392.png 1272w, /__u/substackcdn.com/image/fetch/$s_!NtIA!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92d9a15-cd0d-40cf-a61d-680352cf6c12_1022x1392.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!NtIA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92d9a15-cd0d-40cf-a61d-680352cf6c12_1022x1392.png" width="1022" height="1392" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e92d9a15-cd0d-40cf-a61d-680352cf6c12_1022x1392.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1392,&quot;width&quot;:1022,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:185137,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/186236709?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92d9a15-cd0d-40cf-a61d-680352cf6c12_1022x1392.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!NtIA!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92d9a15-cd0d-40cf-a61d-680352cf6c12_1022x1392.png 424w, /__u/substackcdn.com/image/fetch/$s_!NtIA!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92d9a15-cd0d-40cf-a61d-680352cf6c12_1022x1392.png 848w, /__u/substackcdn.com/image/fetch/$s_!NtIA!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92d9a15-cd0d-40cf-a61d-680352cf6c12_1022x1392.png 1272w, /__u/substackcdn.com/image/fetch/$s_!NtIA!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92d9a15-cd0d-40cf-a61d-680352cf6c12_1022x1392.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4><strong>Java</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!E0oE!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb55d5cab-63be-4c8e-b4da-cef7e2b6e214_1218x1386.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!E0oE!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb55d5cab-63be-4c8e-b4da-cef7e2b6e214_1218x1386.png 424w, /__u/substackcdn.com/image/fetch/$s_!E0oE!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb55d5cab-63be-4c8e-b4da-cef7e2b6e214_1218x1386.png 848w, /__u/substackcdn.com/image/fetch/$s_!E0oE!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb55d5cab-63be-4c8e-b4da-cef7e2b6e214_1218x1386.png 1272w, /__u/substackcdn.com/image/fetch/$s_!E0oE!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb55d5cab-63be-4c8e-b4da-cef7e2b6e214_1218x1386.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!E0oE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb55d5cab-63be-4c8e-b4da-cef7e2b6e214_1218x1386.png" width="1218" height="1386" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b55d5cab-63be-4c8e-b4da-cef7e2b6e214_1218x1386.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1386,&quot;width&quot;:1218,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:203278,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/186236709?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb55d5cab-63be-4c8e-b4da-cef7e2b6e214_1218x1386.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!E0oE!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb55d5cab-63be-4c8e-b4da-cef7e2b6e214_1218x1386.png 424w, /__u/substackcdn.com/image/fetch/$s_!E0oE!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb55d5cab-63be-4c8e-b4da-cef7e2b6e214_1218x1386.png 848w, /__u/substackcdn.com/image/fetch/$s_!E0oE!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb55d5cab-63be-4c8e-b4da-cef7e2b6e214_1218x1386.png 1272w, /__u/substackcdn.com/image/fetch/$s_!E0oE!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb55d5cab-63be-4c8e-b4da-cef7e2b6e214_1218x1386.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4><strong>C++</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!fH9S!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e45cda-0f9b-4a39-b35c-90051234fba3_1228x1386.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!fH9S!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e45cda-0f9b-4a39-b35c-90051234fba3_1228x1386.png 424w, /__u/substackcdn.com/image/fetch/$s_!fH9S!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e45cda-0f9b-4a39-b35c-90051234fba3_1228x1386.png 848w, /__u/substackcdn.com/image/fetch/$s_!fH9S!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e45cda-0f9b-4a39-b35c-90051234fba3_1228x1386.png 1272w, /__u/substackcdn.com/image/fetch/$s_!fH9S!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e45cda-0f9b-4a39-b35c-90051234fba3_1228x1386.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!fH9S!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e45cda-0f9b-4a39-b35c-90051234fba3_1228x1386.png" width="1228" height="1386" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e6e45cda-0f9b-4a39-b35c-90051234fba3_1228x1386.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1386,&quot;width&quot;:1228,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:207556,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/186236709?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e45cda-0f9b-4a39-b35c-90051234fba3_1228x1386.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!fH9S!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e45cda-0f9b-4a39-b35c-90051234fba3_1228x1386.png 424w, /__u/substackcdn.com/image/fetch/$s_!fH9S!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e45cda-0f9b-4a39-b35c-90051234fba3_1228x1386.png 848w, /__u/substackcdn.com/image/fetch/$s_!fH9S!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e45cda-0f9b-4a39-b35c-90051234fba3_1228x1386.png 1272w, /__u/substackcdn.com/image/fetch/$s_!fH9S!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e45cda-0f9b-4a39-b35c-90051234fba3_1228x1386.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h3><strong>Step 4: Answer Follow-Ups</strong></h3><ul><li><p><strong>What if the grid is empty?</strong></p><ul><li><p>Return 0.</p></li><li><p>No cells &#8594; no islands.</p></li></ul></li><li><p><strong>What if we allow diagonal connections?</strong></p><ul><li><p>Add 4 more directions in DFS.</p></li><li><p>Total neighbors = 8 instead of 4.</p></li></ul></li><li><p><strong>What if the grid is very large?</strong></p><ul><li><p>DFS may cause stack overflow.</p></li><li><p>Use BFS or iterative DFS with stack.</p></li></ul></li></ul>]]></content:encoded></item><item><title><![CDATA[Path Sum]]></title><description><![CDATA[LinkedIn Interview Question]]></description><link>https://singhz.substack.com/p/path-sum</link><guid isPermaLink="false">https://singhz.substack.com/p/path-sum</guid><dc:creator><![CDATA[Harpreet Singh]]></dc:creator><pubDate>Mon, 30 Mar 2026 14:01:01 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!BzBp!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F146e4a42-395a-41a0-81d1-ac1c4575626d_1280x720.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!BzBp!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F146e4a42-395a-41a0-81d1-ac1c4575626d_1280x720.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!BzBp!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F146e4a42-395a-41a0-81d1-ac1c4575626d_1280x720.png 424w, /__u/substackcdn.com/image/fetch/$s_!BzBp!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F146e4a42-395a-41a0-81d1-ac1c4575626d_1280x720.png 848w, /__u/substackcdn.com/image/fetch/$s_!BzBp!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F146e4a42-395a-41a0-81d1-ac1c4575626d_1280x720.png 1272w, /__u/substackcdn.com/image/fetch/$s_!BzBp!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F146e4a42-395a-41a0-81d1-ac1c4575626d_1280x720.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!BzBp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F146e4a42-395a-41a0-81d1-ac1c4575626d_1280x720.png" width="1280" height="720" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/146e4a42-395a-41a0-81d1-ac1c4575626d_1280x720.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:720,&quot;width&quot;:1280,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:298340,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/186147196?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F146e4a42-395a-41a0-81d1-ac1c4575626d_1280x720.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!BzBp!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F146e4a42-395a-41a0-81d1-ac1c4575626d_1280x720.png 424w, /__u/substackcdn.com/image/fetch/$s_!BzBp!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F146e4a42-395a-41a0-81d1-ac1c4575626d_1280x720.png 848w, /__u/substackcdn.com/image/fetch/$s_!BzBp!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F146e4a42-395a-41a0-81d1-ac1c4575626d_1280x720.png 1272w, /__u/substackcdn.com/image/fetch/$s_!BzBp!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F146e4a42-395a-41a0-81d1-ac1c4575626d_1280x720.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3><strong>Problem Overview</strong></h3><blockquote><p><strong>Difficulty: </strong>Easy<br><strong>LeetCode Pattern:</strong> DFS</p></blockquote><p>Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum.</p><p><em>Note: A leaf is a node with no children.</em></p><pre><code><strong>Tree:</strong>
    1
   / \
  2   3
 /     \
4       5

<strong>Example 1:</strong>
&#183; Input: targetSum = 9
&#183; Output: true

<strong>Example 2:</strong>
&#183; Input: targetSum = 2
&#183; Output: false</code></pre><div><hr></div><h3><strong>Step 1: Clarify Requirements</strong></h3><ul><li><p><strong>Can the tree be empty?</strong></p><ul><li><p>Yes, it can be empty. </p></li><li><p>Return false in that case.</p></li></ul></li><li><p><strong>Can node values be negative?</strong></p><ul><li><p>Yes, they can be negative.</p></li></ul></li><li><p><strong>Can targetSum be negative?</strong></p><ul><li><p>Yes, targetSum can be negative.</p></li></ul></li><li><p><strong>Do we need to return the path?</strong></p><ul><li><p>No, only return true or false.</p></li></ul></li></ul><div><hr></div><h3><strong>Step 2: Discuss Approaches</strong></h3><p><strong>1/ Check All Paths (Suboptimal &#9888;&#65039;)</strong></p><ul><li><p><strong>Logic:</strong></p><ul><li><p>Find all root-to-leaf paths</p></li><li><p>For each path:</p><ul><li><p>Compute sum of node values</p></li><li><p>Check if sum equals targetSum</p></li></ul></li><li><p>If any path matches &#8594; return true</p></li></ul></li><li><p><strong>Big O:</strong></p><ul><li><p>Time Complexity: O(n&#178;)</p></li><li><p>Space Complexity: O(h)</p></li></ul></li></ul><div><hr></div><p><strong>2/ Use DFS (Optimal &#9989;)</strong></p><ul><li><p><strong>Logic:</strong></p><ul><li><p>Use DFS starting from root</p></li><li><p>Pass remainingSum = targetSum</p></li><li><p>At each node:</p><ul><li><p>Do remainingSum -= node.val</p></li></ul></li><li><p>If at a leaf node:</p><ul><li><p>Check if remainingSum == 0</p></li><li><p>If yes &#8594; return true</p></li></ul></li><li><p>Recursively check left and right:</p><ul><li><p>Pass updated remainingSum</p></li></ul></li><li><p>If any path returns true:</p><ul><li><p>return true</p></li></ul></li><li><p>Otherwise:</p><ul><li><p>return false</p></li></ul></li></ul></li><li><p><strong>Big O:</strong></p><ul><li><p>Time Complexity: O(n)</p></li><li><p>Space Complexity: O(h)</p></li></ul></li></ul><div><hr></div><h3><strong>Step 3: Write Code (Optimal)</strong></h3><h4><strong>Python</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!9-jg!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5140832c-0fac-4d2d-95d7-4bcb033b26d6_1038x640.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!9-jg!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5140832c-0fac-4d2d-95d7-4bcb033b26d6_1038x640.png 424w, /__u/substackcdn.com/image/fetch/$s_!9-jg!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5140832c-0fac-4d2d-95d7-4bcb033b26d6_1038x640.png 848w, /__u/substackcdn.com/image/fetch/$s_!9-jg!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5140832c-0fac-4d2d-95d7-4bcb033b26d6_1038x640.png 1272w, /__u/substackcdn.com/image/fetch/$s_!9-jg!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5140832c-0fac-4d2d-95d7-4bcb033b26d6_1038x640.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!9-jg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5140832c-0fac-4d2d-95d7-4bcb033b26d6_1038x640.png" width="1038" height="640" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5140832c-0fac-4d2d-95d7-4bcb033b26d6_1038x640.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:640,&quot;width&quot;:1038,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:109836,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/186147196?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5140832c-0fac-4d2d-95d7-4bcb033b26d6_1038x640.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!9-jg!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5140832c-0fac-4d2d-95d7-4bcb033b26d6_1038x640.png 424w, /__u/substackcdn.com/image/fetch/$s_!9-jg!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5140832c-0fac-4d2d-95d7-4bcb033b26d6_1038x640.png 848w, /__u/substackcdn.com/image/fetch/$s_!9-jg!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5140832c-0fac-4d2d-95d7-4bcb033b26d6_1038x640.png 1272w, /__u/substackcdn.com/image/fetch/$s_!9-jg!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5140832c-0fac-4d2d-95d7-4bcb033b26d6_1038x640.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4><strong>Java</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!AK8Q!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe5cdf9b-a8cf-44f5-ba75-9e6e3bcf7571_1064x608.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!AK8Q!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe5cdf9b-a8cf-44f5-ba75-9e6e3bcf7571_1064x608.png 424w, /__u/substackcdn.com/image/fetch/$s_!AK8Q!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe5cdf9b-a8cf-44f5-ba75-9e6e3bcf7571_1064x608.png 848w, /__u/substackcdn.com/image/fetch/$s_!AK8Q!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe5cdf9b-a8cf-44f5-ba75-9e6e3bcf7571_1064x608.png 1272w, /__u/substackcdn.com/image/fetch/$s_!AK8Q!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe5cdf9b-a8cf-44f5-ba75-9e6e3bcf7571_1064x608.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!AK8Q!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe5cdf9b-a8cf-44f5-ba75-9e6e3bcf7571_1064x608.png" width="1064" height="608" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/be5cdf9b-a8cf-44f5-ba75-9e6e3bcf7571_1064x608.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:608,&quot;width&quot;:1064,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:112070,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/186147196?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe5cdf9b-a8cf-44f5-ba75-9e6e3bcf7571_1064x608.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!AK8Q!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe5cdf9b-a8cf-44f5-ba75-9e6e3bcf7571_1064x608.png 424w, /__u/substackcdn.com/image/fetch/$s_!AK8Q!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe5cdf9b-a8cf-44f5-ba75-9e6e3bcf7571_1064x608.png 848w, /__u/substackcdn.com/image/fetch/$s_!AK8Q!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe5cdf9b-a8cf-44f5-ba75-9e6e3bcf7571_1064x608.png 1272w, /__u/substackcdn.com/image/fetch/$s_!AK8Q!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe5cdf9b-a8cf-44f5-ba75-9e6e3bcf7571_1064x608.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4><strong>C++</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!pN6D!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febaecaf3-9b0b-467f-894c-310c6e1e215c_1084x610.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!pN6D!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febaecaf3-9b0b-467f-894c-310c6e1e215c_1084x610.png 424w, /__u/substackcdn.com/image/fetch/$s_!pN6D!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febaecaf3-9b0b-467f-894c-310c6e1e215c_1084x610.png 848w, /__u/substackcdn.com/image/fetch/$s_!pN6D!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febaecaf3-9b0b-467f-894c-310c6e1e215c_1084x610.png 1272w, /__u/substackcdn.com/image/fetch/$s_!pN6D!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febaecaf3-9b0b-467f-894c-310c6e1e215c_1084x610.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!pN6D!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febaecaf3-9b0b-467f-894c-310c6e1e215c_1084x610.png" width="1084" height="610" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ebaecaf3-9b0b-467f-894c-310c6e1e215c_1084x610.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:610,&quot;width&quot;:1084,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:113488,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/186147196?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febaecaf3-9b0b-467f-894c-310c6e1e215c_1084x610.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!pN6D!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febaecaf3-9b0b-467f-894c-310c6e1e215c_1084x610.png 424w, /__u/substackcdn.com/image/fetch/$s_!pN6D!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febaecaf3-9b0b-467f-894c-310c6e1e215c_1084x610.png 848w, /__u/substackcdn.com/image/fetch/$s_!pN6D!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febaecaf3-9b0b-467f-894c-310c6e1e215c_1084x610.png 1272w, /__u/substackcdn.com/image/fetch/$s_!pN6D!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febaecaf3-9b0b-467f-894c-310c6e1e215c_1084x610.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h3><strong>Step 4: Answer Follow-Ups</strong></h3><ul><li><p><strong>What if the tree is empty?</strong></p><ul><li><p>Return false.</p></li><li><p>No root-to-leaf path exists.</p></li></ul></li><li><p><strong>What if the targetSum is 0?</strong></p><ul><li><p>Still need to check all paths.</p></li><li><p>Return true if path sum == 0.</p></li></ul></li><li><p><strong>What if node values are negative?</strong></p><ul><li><p>Our solutions works fine.</p></li><li><p>DFS handles negatives naturally.</p></li></ul></li><li><p><strong>Can we stop early?</strong></p><ul><li><p>Our solution does stop early.</p></li><li><p>It returns once it finds a valid path.</p></li></ul></li></ul>]]></content:encoded></item><item><title><![CDATA[Rotting Oranges]]></title><description><![CDATA[Salesforce Interview Question]]></description><link>https://singhz.substack.com/p/rotting-oranges</link><guid isPermaLink="false">https://singhz.substack.com/p/rotting-oranges</guid><dc:creator><![CDATA[Harpreet Singh]]></dc:creator><pubDate>Mon, 23 Mar 2026 14:00:35 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!Vtrn!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F174ff915-0989-4127-977d-8d900eebb782_1280x720.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Vtrn!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F174ff915-0989-4127-977d-8d900eebb782_1280x720.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Vtrn!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F174ff915-0989-4127-977d-8d900eebb782_1280x720.png 424w, /__u/substackcdn.com/image/fetch/$s_!Vtrn!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F174ff915-0989-4127-977d-8d900eebb782_1280x720.png 848w, /__u/substackcdn.com/image/fetch/$s_!Vtrn!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F174ff915-0989-4127-977d-8d900eebb782_1280x720.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Vtrn!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F174ff915-0989-4127-977d-8d900eebb782_1280x720.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Vtrn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F174ff915-0989-4127-977d-8d900eebb782_1280x720.png" width="1280" height="720" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/174ff915-0989-4127-977d-8d900eebb782_1280x720.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:720,&quot;width&quot;:1280,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:314733,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/186145524?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F174ff915-0989-4127-977d-8d900eebb782_1280x720.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!Vtrn!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F174ff915-0989-4127-977d-8d900eebb782_1280x720.png 424w, /__u/substackcdn.com/image/fetch/$s_!Vtrn!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F174ff915-0989-4127-977d-8d900eebb782_1280x720.png 848w, /__u/substackcdn.com/image/fetch/$s_!Vtrn!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F174ff915-0989-4127-977d-8d900eebb782_1280x720.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Vtrn!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F174ff915-0989-4127-977d-8d900eebb782_1280x720.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3><strong>Problem Overview</strong></h3><blockquote><p><strong>Difficulty: </strong>Medium<br><strong>LeetCode Pattern:</strong> BFS</p></blockquote><p>You are given an m x n grid where each cell can have one of three values:</p><ul><li><p>0 representing an empty cell,</p></li><li><p>1 representing a fresh orange, or</p></li><li><p>2 representing a rotten orange.</p></li></ul><p>Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten.</p><p>Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1.</p><p><strong>Example</strong></p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!_9KX!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58ff82d6-fb98-46e6-9b4b-7cd2ebb91182_1328x318.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!_9KX!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58ff82d6-fb98-46e6-9b4b-7cd2ebb91182_1328x318.png 424w, /__u/substackcdn.com/image/fetch/$s_!_9KX!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58ff82d6-fb98-46e6-9b4b-7cd2ebb91182_1328x318.png 848w, /__u/substackcdn.com/image/fetch/$s_!_9KX!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58ff82d6-fb98-46e6-9b4b-7cd2ebb91182_1328x318.png 1272w, /__u/substackcdn.com/image/fetch/$s_!_9KX!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58ff82d6-fb98-46e6-9b4b-7cd2ebb91182_1328x318.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!_9KX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58ff82d6-fb98-46e6-9b4b-7cd2ebb91182_1328x318.png" width="1328" height="318" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/58ff82d6-fb98-46e6-9b4b-7cd2ebb91182_1328x318.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:318,&quot;width&quot;:1328,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:204363,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/186145524?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58ff82d6-fb98-46e6-9b4b-7cd2ebb91182_1328x318.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!_9KX!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58ff82d6-fb98-46e6-9b4b-7cd2ebb91182_1328x318.png 424w, /__u/substackcdn.com/image/fetch/$s_!_9KX!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58ff82d6-fb98-46e6-9b4b-7cd2ebb91182_1328x318.png 848w, /__u/substackcdn.com/image/fetch/$s_!_9KX!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58ff82d6-fb98-46e6-9b4b-7cd2ebb91182_1328x318.png 1272w, /__u/substackcdn.com/image/fetch/$s_!_9KX!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58ff82d6-fb98-46e6-9b4b-7cd2ebb91182_1328x318.png 1456w" sizes="100vw"></picture><div></div></div></a></figure></div><pre><code><strong>Input:</strong>
grid = 
[[2,1,1],
 [1,1,0],
 [0,1,1]]

<strong>Output:</strong>
&#183; 4</code></pre><div><hr></div><h3><strong>Step 1: Clarify Requirements</strong></h3><ul><li><p><strong>Can the grid be empty?</strong></p><ul><li><p>Yes, it can be empty. </p></li><li><p>Return 0 in that case.</p></li></ul></li><li><p><strong>Can fresh oranges be unreachable?</strong></p><ul><li><p>Yes, that is possible.</p></li><li><p>Return -1 if that happens.</p></li></ul></li><li><p><strong>Can many oranges be rotten initially?</strong></p><ul><li><p>Yes, that is possible.</p></li><li><p>Rotting starts from all those places.</p></li></ul></li></ul><div><hr></div><h3><strong>Step 2: Discuss Approaches</strong></h3><p><strong>1/ One-By-One (Brute Force &#9888;&#65039;)</strong></p><ul><li><p><strong>Logic:</strong></p><ul><li><p>For each minute:</p><ul><li><p>Scan the grid</p></li><li><p>Find fresh orange</p></li><li><p>If next to a rotten orange:</p><ul><li><p>Rot this fresh orange</p></li></ul></li></ul></li><li><p>Repeat till no fresh oranges can rot</p></li></ul></li><li><p><strong>Big O:</strong></p><ul><li><p>Time Complexity: O(m&#183;n)&#178;</p></li><li><p>Space Complexity: O(1)</p></li></ul></li></ul><div><hr></div><p><strong>2/ Multi-Source BFS (Optimal &#9989;)</strong></p><ul><li><p><strong>Logic:</strong></p><ul><li><p>Use a queue</p></li><li><p>Store <strong>all rotten oranges </strong>initially</p></li><li><p>Track <strong>minutes elapsed</strong></p></li><li><p>While queue not empty:</p><ul><li><p>Process all rotten oranges</p></li><li><p>For each rotten orange:</p><ul><li><p>Rot adjacent fresh oranges</p></li><li><p>Add these to the queue</p></li></ul></li></ul></li><li><p>After BFS finishes:</p><ul><li><p>check if fresh oranges remain</p></li><li><p>If yes, <strong>return -1</strong></p></li><li><p>If no, <strong>return minutes elapsed</strong></p></li></ul></li></ul></li><li><p><strong>Big O:</strong></p><ul><li><p>Time Complexity: O(m&#183;n)</p></li><li><p>Space Complexity: O(m&#183;n)</p></li></ul></li></ul><div><hr></div><h3><strong>Step 3: Write Code (Optimal)</strong></h3><h4><strong>Python</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!1pW9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a218736-13ec-4310-9e8b-2cb9887e59fd_1366x1506.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!1pW9!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a218736-13ec-4310-9e8b-2cb9887e59fd_1366x1506.png 424w, /__u/substackcdn.com/image/fetch/$s_!1pW9!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a218736-13ec-4310-9e8b-2cb9887e59fd_1366x1506.png 848w, /__u/substackcdn.com/image/fetch/$s_!1pW9!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a218736-13ec-4310-9e8b-2cb9887e59fd_1366x1506.png 1272w, /__u/substackcdn.com/image/fetch/$s_!1pW9!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a218736-13ec-4310-9e8b-2cb9887e59fd_1366x1506.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!1pW9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a218736-13ec-4310-9e8b-2cb9887e59fd_1366x1506.png" width="1366" height="1506" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2a218736-13ec-4310-9e8b-2cb9887e59fd_1366x1506.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1506,&quot;width&quot;:1366,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:273546,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/186145524?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a218736-13ec-4310-9e8b-2cb9887e59fd_1366x1506.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!1pW9!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a218736-13ec-4310-9e8b-2cb9887e59fd_1366x1506.png 424w, /__u/substackcdn.com/image/fetch/$s_!1pW9!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a218736-13ec-4310-9e8b-2cb9887e59fd_1366x1506.png 848w, /__u/substackcdn.com/image/fetch/$s_!1pW9!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a218736-13ec-4310-9e8b-2cb9887e59fd_1366x1506.png 1272w, /__u/substackcdn.com/image/fetch/$s_!1pW9!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a218736-13ec-4310-9e8b-2cb9887e59fd_1366x1506.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4><strong>Java</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!lB0c!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F405adf67-d898-4b78-bed6-da1afe40454c_1500x1532.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!lB0c!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F405adf67-d898-4b78-bed6-da1afe40454c_1500x1532.png 424w, /__u/substackcdn.com/image/fetch/$s_!lB0c!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F405adf67-d898-4b78-bed6-da1afe40454c_1500x1532.png 848w, /__u/substackcdn.com/image/fetch/$s_!lB0c!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F405adf67-d898-4b78-bed6-da1afe40454c_1500x1532.png 1272w, /__u/substackcdn.com/image/fetch/$s_!lB0c!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F405adf67-d898-4b78-bed6-da1afe40454c_1500x1532.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!lB0c!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F405adf67-d898-4b78-bed6-da1afe40454c_1500x1532.png" width="1456" height="1487" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/405adf67-d898-4b78-bed6-da1afe40454c_1500x1532.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1487,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:299115,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/186145524?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F405adf67-d898-4b78-bed6-da1afe40454c_1500x1532.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!lB0c!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F405adf67-d898-4b78-bed6-da1afe40454c_1500x1532.png 424w, /__u/substackcdn.com/image/fetch/$s_!lB0c!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F405adf67-d898-4b78-bed6-da1afe40454c_1500x1532.png 848w, /__u/substackcdn.com/image/fetch/$s_!lB0c!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F405adf67-d898-4b78-bed6-da1afe40454c_1500x1532.png 1272w, /__u/substackcdn.com/image/fetch/$s_!lB0c!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F405adf67-d898-4b78-bed6-da1afe40454c_1500x1532.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4><strong>C++</strong></h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!nn1W!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb293a080-a02d-42c0-b63d-4846fe327b0a_1534x1504.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!nn1W!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb293a080-a02d-42c0-b63d-4846fe327b0a_1534x1504.png 424w, /__u/substackcdn.com/image/fetch/$s_!nn1W!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb293a080-a02d-42c0-b63d-4846fe327b0a_1534x1504.png 848w, /__u/substackcdn.com/image/fetch/$s_!nn1W!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb293a080-a02d-42c0-b63d-4846fe327b0a_1534x1504.png 1272w, /__u/substackcdn.com/image/fetch/$s_!nn1W!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_webp, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb293a080-a02d-42c0-b63d-4846fe327b0a_1534x1504.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!nn1W!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb293a080-a02d-42c0-b63d-4846fe327b0a_1534x1504.png" width="1456" height="1428" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b293a080-a02d-42c0-b63d-4846fe327b0a_1534x1504.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1428,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:286027,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://singhz.substack.com/i/186145524?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb293a080-a02d-42c0-b63d-4846fe327b0a_1534x1504.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!nn1W!, /__u/singhz.substack.com/w_424, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb293a080-a02d-42c0-b63d-4846fe327b0a_1534x1504.png 424w, /__u/substackcdn.com/image/fetch/$s_!nn1W!, /__u/singhz.substack.com/w_848, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb293a080-a02d-42c0-b63d-4846fe327b0a_1534x1504.png 848w, /__u/substackcdn.com/image/fetch/$s_!nn1W!, /__u/singhz.substack.com/w_1272, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb293a080-a02d-42c0-b63d-4846fe327b0a_1534x1504.png 1272w, /__u/substackcdn.com/image/fetch/$s_!nn1W!, /__u/singhz.substack.com/w_1456, /__u/singhz.substack.com/c_limit, /__u/singhz.substack.com/f_auto, /__u/singhz.substack.com/q_auto:good, /__u/singhz.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb293a080-a02d-42c0-b63d-4846fe327b0a_1534x1504.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h3><strong>Step 4: Answer Follow-Ups</strong></h3><ul><li><p><strong>What if the grid is empty?</strong></p><ul><li><p>Return 0.</p></li><li><p>No oranges to rot.</p></li></ul></li><li><p><strong>What if we allow diagonal spread?</strong></p><ul><li><p>Add 4 diagonal checks also.</p></li><li><p>BFS will still work correctly.</p></li></ul></li></ul>]]></content:encoded></item></channel></rss>