<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tomer Ben David</title>
    <description>The latest articles on DEV Community by Tomer Ben David (@tomerbendavid).</description>
    <link>https://dev.to/tomerbendavid</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1528%2F47ca83b6-b329-434d-a98f-79851ae130ef.png</url>
      <title>DEV Community: Tomer Ben David</title>
      <link>https://dev.to/tomerbendavid</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tomerbendavid"/>
    <language>en</language>
    <item>
      <title>The Living Giant Python Syntax and Traps LeetCode Document</title>
      <dc:creator>Tomer Ben David</dc:creator>
      <pubDate>Fri, 15 May 2026 09:45:51 +0000</pubDate>
      <link>https://dev.to/tomerbendavid/the-living-giant-python-syntax-and-traps-leetcode-document-511h</link>
      <guid>https://dev.to/tomerbendavid/the-living-giant-python-syntax-and-traps-leetcode-document-511h</guid>
      <description>&lt;p&gt;Yes, this document is long and that is entirely by design. It serves as the ultimate all in one compilation of every technical Python tip, idiom, syntax pattern, and common trap you will encounter when solving LeetCode questions. It is designed to be your continuously updatable single source of truth. The goal is to help you internalize syntax so completely that you can focus all your mental energy entirely on algorithmic logic and problem solving during high pressure interviews.&lt;/p&gt;

&lt;p&gt;The document is organized strictly all in prespecitve of Python syntax fundamentals to advanced algorithmic structures for solving leetcode question.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Variables &amp;amp; Syntax Basics&lt;/li&gt;
&lt;li&gt;Math &amp;amp; Numbers&lt;/li&gt;
&lt;li&gt;Strings &amp;amp; Characters&lt;/li&gt;
&lt;li&gt;Iteration &amp;amp; Loops&lt;/li&gt;
&lt;li&gt;Lists &amp;amp; Arrays&lt;/li&gt;
&lt;li&gt;Dictionaries &amp;amp; Sets&lt;/li&gt;
&lt;li&gt;Queues &amp;amp; Stacks&lt;/li&gt;
&lt;li&gt;Heaps&lt;/li&gt;
&lt;li&gt;Linked Lists&lt;/li&gt;
&lt;li&gt;Trees &amp;amp; Graphs&lt;/li&gt;
&lt;li&gt;Advanced Patterns (Intervals, Sliding Window)&lt;/li&gt;
&lt;li&gt;Recursion &amp;amp; Caching&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Variables &amp;amp; Syntax Basics
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Unpacking
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Unpack list/tuple
&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;# Unpack with rest
&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;rest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  &lt;span class="c1"&gt;# first=1, rest=[2,3,4,5]
&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  &lt;span class="c1"&gt;# start=[1,2,3,4], last=5
&lt;/span&gt;
&lt;span class="c1"&gt;# Unpack in loop
&lt;/span&gt;&lt;span class="n"&gt;queries&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;queries&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Slicing
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;      &lt;span class="c1"&gt;# [1, 2, 3]
&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;       &lt;span class="c1"&gt;# [0, 1, 2] (from start)
&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;:]&lt;/span&gt;       &lt;span class="c1"&gt;# [3, 4, 5] (to end)
&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[::&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;     &lt;span class="c1"&gt;# [5, 4, 3, 2, 1, 0] (reverse)
&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[::&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;      &lt;span class="c1"&gt;# [0, 2, 4] (every 2nd element)
&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;       &lt;span class="c1"&gt;# Last element
&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;       &lt;span class="c1"&gt;# Second to last element
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Mutable vs Immutable Multiplication (* n)
&lt;/h3&gt;

&lt;p&gt;One of the most common Python traps is using the &lt;code&gt;*&lt;/code&gt; operator to initialize a list of lists (or any container).&lt;/p&gt;

&lt;h3&gt;
  
  
  The Trap: &lt;code&gt;[[]] * n&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This does &lt;strong&gt;not&lt;/strong&gt; create &lt;code&gt;n&lt;/code&gt; independent empty lists. In Python, &lt;code&gt;list * n&lt;/code&gt; is essentially &lt;strong&gt;repeated concatenation (&lt;code&gt;+&lt;/code&gt;)&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Create 3 "references" to the SAME inner list
&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[]]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; 
&lt;span class="c1"&gt;# is equivalent to: g = [list_a] + [list_a] + [list_a]
&lt;/span&gt;
&lt;span class="c1"&gt;# Add a neighbor to node 0
&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="c1"&gt;# Expected: [[1], [], []]
# Actual:   [[1], [1], [1]]  &amp;lt;--  Every index changed!
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The "Double List" Principle&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[] * 3&lt;/code&gt; is &lt;code&gt;[] + [] + []&lt;/code&gt; which results in an empty list &lt;code&gt;[]&lt;/code&gt;. (Concatenating "nothing" is still nothing).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[[]] * 3&lt;/code&gt; is &lt;code&gt;[ref] + [ref] + [ref]&lt;/code&gt; which results in a list of 3 pointers to the &lt;strong&gt;same&lt;/strong&gt; memory address.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Fix: List Comprehension
&lt;/h3&gt;

&lt;p&gt;To create independent mutable objects, you must use a list comprehension.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[]&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;

&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Output: [[1], [], []] &amp;lt;--  Independent!
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Why is &lt;code&gt;[False] * n&lt;/code&gt; safe?
&lt;/h3&gt;

&lt;p&gt;You might notice we often use &lt;code&gt;seen = [False] * n&lt;/code&gt;. Why doesn't this break?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Immutable types&lt;/strong&gt; (integers, booleans, strings) cannot be mutated. &lt;/li&gt;
&lt;li&gt;When you do &lt;code&gt;seen[0] = True&lt;/code&gt;, you aren't "changing" the &lt;code&gt;False&lt;/code&gt; object; you are &lt;strong&gt;replacing&lt;/strong&gt; the reference at index 0 with a completely new object (&lt;code&gt;True&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mutable types&lt;/strong&gt; (lists, dicts, sets) can be changed in-place (e.g., &lt;code&gt;.append()&lt;/code&gt;, &lt;code&gt;.add()&lt;/code&gt;). These changes are seen by all references pointing to that object.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Mental Rule
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt; &lt;code&gt;[constant] * n&lt;/code&gt; is safe for &lt;strong&gt;immutable&lt;/strong&gt; primitives (int, bool, str, None).&lt;/li&gt;
&lt;li&gt; &lt;code&gt;[container] * n&lt;/code&gt; is dangerous for &lt;strong&gt;mutable&lt;/strong&gt; objects (list, dict, set). &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Always use a list comprehension for graph adjacency lists or 2D matrices.&lt;/strong&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Grid Boundary Helper: &lt;code&gt;valid()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;When working with matrices or grids, defining a &lt;code&gt;valid()&lt;/code&gt; helper function makes your code much cleaner and less error-prone.&lt;/p&gt;

&lt;h3&gt;
  
  
  Nested Boundary Helper
&lt;/h3&gt;

&lt;p&gt;Instead of repeating a complex &lt;code&gt;if&lt;/code&gt; condition in every loop, define a helper (often a nested function) to handle boundary checks and even visited/blocked status.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;solve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;ROWS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;COLS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;is_valid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;ROWS&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;COLS&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;BLOCKED&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

    &lt;span class="c1"&gt;# Use it everywhere
&lt;/span&gt;    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ROWS&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;COLS&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;is_valid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
                &lt;span class="c1"&gt;# logic...
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Safety and Readability
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Readability&lt;/strong&gt;: &lt;code&gt;if is_valid(nr, nc):&lt;/code&gt; reads like English.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Maintainability&lt;/strong&gt;: If you need to add a condition (e.g., "don't walk into walls"), you only change it in &lt;strong&gt;one place&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Error Reduction&lt;/strong&gt;: Prevents "off-by-one" errors or swapping &lt;code&gt;ROWS&lt;/code&gt; and &lt;code&gt;COLS&lt;/code&gt; in multiple locations.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Nested Visibility&lt;/strong&gt;: By defining it inside your main function, it automatically has access to &lt;code&gt;ROWS&lt;/code&gt;, &lt;code&gt;COLS&lt;/code&gt;, and &lt;code&gt;grid&lt;/code&gt; via closure.&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Lambda Functions
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Anonymous function
&lt;/span&gt;&lt;span class="n"&gt;square&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;

&lt;span class="c1"&gt;# Often used with map, filter, sorted
&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;squared&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;evens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;sorted_by_abs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Truthiness Pitfalls
&lt;/h3&gt;

&lt;p&gt;In Python, many values evaluate to &lt;code&gt;False&lt;/code&gt; in a boolean context. This is called "falsiness".&lt;/p&gt;

&lt;h3&gt;
  
  
  The Danger
&lt;/h3&gt;

&lt;p&gt;If you are checking if a variable exists or has been assigned, and that variable could be &lt;code&gt;0&lt;/code&gt;, &lt;code&gt;""&lt;/code&gt;, or &lt;code&gt;[]&lt;/code&gt;, using &lt;code&gt;if not x:&lt;/code&gt; will lead to bugs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;find_closest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;best_val&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# BAD: if node.val is 0, this logic might incorrectly skip it
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;best_val&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;best_val&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;best_val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The Solution: Explicit &lt;code&gt;None&lt;/code&gt; Check
&lt;/h3&gt;

&lt;p&gt;Always use &lt;code&gt;is None&lt;/code&gt; or &lt;code&gt;is not None&lt;/code&gt; when &lt;code&gt;0&lt;/code&gt; is a valid piece of data (like in LeetCode tree/array problems).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;find_closest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;best_val&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# GOOD: Explicitly check for None
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;best_val&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;best_val&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;best_val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Falsy Values in Python
&lt;/h3&gt;

&lt;p&gt;The following are all "Falsy":&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;None&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;False&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;0&lt;/code&gt; (int)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;0.0&lt;/code&gt; (float)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;""&lt;/code&gt; (empty string)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[]&lt;/code&gt; (empty list)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;{}&lt;/code&gt; (empty dict)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;set()&lt;/code&gt; (empty set)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Interview Tip
&lt;/h3&gt;

&lt;p&gt;If you're writing a tree problem and you catch yourself writing &lt;code&gt;if not left:&lt;/code&gt;, ask yourself: &lt;strong&gt;"Could the left-subtree-result be 0?"&lt;/strong&gt;. If yes, change it to &lt;code&gt;if left is None:&lt;/code&gt;. This single habit prevents many "silent" bugs that are hard to debug during an interview.&lt;/p&gt;




&lt;h3&gt;
  
  
  Collection Truthiness (Implicit Empty Check)
&lt;/h3&gt;

&lt;p&gt;In Python, sequences (lists, strings, tuples) and collections (sets, dictionaries) are "falsy" if they are empty and "truthy" if they contain at least one element.&lt;/p&gt;

&lt;h3&gt;
  
  
  Idiomatic Way
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

&lt;span class="c1"&gt;# To check if empty:
&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Stack is empty&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# To check if NOT empty (has elements):
&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Stack has elements&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Very common in loops:
&lt;/span&gt;&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="c1"&gt;# process item
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Less Idiomatic
&lt;/h3&gt;

&lt;p&gt;Avoid checking length explicitly unless you actually need the count.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;# Use 'if not stack' instead
&lt;/span&gt;    &lt;span class="bp"&gt;...&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;   &lt;span class="c1"&gt;# Use 'if stack' instead
&lt;/span&gt;    &lt;span class="bp"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Using Underscore for Unused Loop Variables
&lt;/h3&gt;

&lt;p&gt;In Python, it is a common convention to use an underscore (&lt;code&gt;_&lt;/code&gt;) as a variable name when you need to loop for a specific number of times but don't actually need the index within the loop body.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example: Finding the kth Node from the End
&lt;/h3&gt;

&lt;p&gt;This technique is often used in linked list patterns, such as finding the kth node from the end.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;find_node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;slow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;
    &lt;span class="n"&gt;fast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;

    &lt;span class="c1"&gt;# Use _ because we don't need the index value
&lt;/span&gt;    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;fast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;slow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;slow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;
        &lt;span class="n"&gt;fast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;slow&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why Use &lt;code&gt;_&lt;/code&gt;?
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Readability&lt;/strong&gt;: It explicitly signals to other developers (and your future self) that the loop index is intentionally ignored.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Linting&lt;/strong&gt;: Many linters will warn about unused variables. Using &lt;code&gt;_&lt;/code&gt; is the standard way to bypass these warnings for loop indices.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Clarity&lt;/strong&gt;: It keeps the focus on the loop's purpose (repetitive action) rather than the iteration state.&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Swap Two Variables (No Temp Variable Needed)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Traditional swap with temp variable
&lt;/span&gt;&lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;

&lt;span class="c1"&gt;# Python's elegant way using tuple unpacking
&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;

&lt;span class="c1"&gt;# Swap array elements
&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Useful Built-in Functions
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;   &lt;span class="c1"&gt;# False (all must be True)
&lt;/span&gt;&lt;span class="nf"&gt;any&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;  &lt;span class="c1"&gt;# True (any must be True)
&lt;/span&gt;&lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;             &lt;span class="c1"&gt;# 6
&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;             &lt;span class="c1"&gt;# 3
&lt;/span&gt;&lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                   &lt;span class="c1"&gt;# 0, 1, 2, 3, 4 (stops before 5)
&lt;/span&gt;&lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                &lt;span class="c1"&gt;# 1, 2, 3, 4 (start inclusive, end exclusive)
&lt;/span&gt;&lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;            &lt;span class="c1"&gt;# 0, 2, 4, 6, 8 (step by 2)
&lt;/span&gt;&lt;span class="nf"&gt;bin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                   &lt;span class="c1"&gt;# '0b101' (binary string)
&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;110&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;1&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;         &lt;span class="c1"&gt;# 2 (count occurrences in string)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Bit manipulation tricks:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;i &amp;gt;&amp;gt; 1&lt;/code&gt;: Shift right by 1 (same as &lt;code&gt;i // 2&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;i &amp;amp; 1&lt;/code&gt;: Get the last bit (same as &lt;code&gt;i % 2&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Common Python Syntax Pitfalls
&lt;/h3&gt;

&lt;h3&gt;
  
  
  1. Operator Precedence (The Midpoint Bug)
&lt;/h3&gt;

&lt;p&gt;When calculating the middle of two numbers, the order of operations matters.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;[!CAUTION]&lt;br&gt;
&lt;strong&gt;Wrong&lt;/strong&gt;: &lt;code&gt;mid = low + high // 2&lt;/code&gt;&lt;br&gt;
Python sees &lt;code&gt;high // 2&lt;/code&gt; first, then adds it to &lt;code&gt;low&lt;/code&gt;. Example: &lt;code&gt;low=10, high=20&lt;/code&gt;. Calculation: &lt;code&gt;10 + (20 // 2) = 20&lt;/code&gt;. You never found the middle!&lt;/p&gt;

&lt;p&gt;[!TIP]&lt;br&gt;
&lt;strong&gt;Right&lt;/strong&gt;: &lt;code&gt;mid = (low + high) // 2&lt;/code&gt;&lt;br&gt;
The parentheses force the addition to happen first. Or even better: &lt;code&gt;mid = low + (high - low) // 2&lt;/code&gt; to avoid integer overflow.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  2. Generator Expressions with &lt;code&gt;sum()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Python allows you to sum up items in a single line, but the placement of the loop matters.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;[!CAUTION]&lt;br&gt;
&lt;strong&gt;Wrong&lt;/strong&gt;: &lt;code&gt;total = sum(math.ceil(x / y)) for x in items&lt;/code&gt;&lt;br&gt;
This tries to call &lt;code&gt;sum()&lt;/code&gt; on a single number, then tries to start a loop afterward.&lt;/p&gt;

&lt;p&gt;[!TIP]&lt;br&gt;
&lt;strong&gt;Right&lt;/strong&gt;: &lt;code&gt;total = sum(math.ceil(x / y) for x in items)&lt;/code&gt;&lt;br&gt;
The entire &lt;code&gt;... for ... in ...&lt;/code&gt; expression must be &lt;strong&gt;inside&lt;/strong&gt; the &lt;code&gt;sum()&lt;/code&gt; parentheses. This is called a "generator expression." It's fast and memory-efficient.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  3. Floor Division (&lt;code&gt;//&lt;/code&gt;) vs. True Division (&lt;code&gt;/&lt;/code&gt;)
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;[!CAUTION]&lt;br&gt;
&lt;strong&gt;The Trap&lt;/strong&gt;: If you use &lt;code&gt;range()&lt;/code&gt;, &lt;code&gt;list[index]&lt;/code&gt;, or binary search pointers, you &lt;strong&gt;MUST&lt;/strong&gt; use an integer. Using &lt;code&gt;/&lt;/code&gt; will cause a &lt;code&gt;TypeError&lt;/code&gt; because it always returns a &lt;strong&gt;float&lt;/strong&gt; (e.g., &lt;code&gt;4 / 2 = 2.0&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;[!TIP]&lt;br&gt;
&lt;strong&gt;The Fix&lt;/strong&gt;: Use floor division &lt;code&gt;//&lt;/code&gt; to ensure you get an &lt;strong&gt;integer&lt;/strong&gt; (e.g., &lt;code&gt;5 // 2 = 2&lt;/code&gt;).&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  4. &lt;code&gt;list.append()&lt;/code&gt; and &lt;code&gt;list.sort()&lt;/code&gt; return &lt;code&gt;None&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;In Python, methods that modify a list &lt;strong&gt;in-place&lt;/strong&gt; return &lt;code&gt;None&lt;/code&gt;. You cannot chain them.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;[!CAUTION]&lt;br&gt;
&lt;strong&gt;Wrong&lt;/strong&gt;: &lt;code&gt;result.append(val).count('1')&lt;/code&gt;&lt;br&gt;
&lt;code&gt;result.append(val)&lt;/code&gt; returns &lt;code&gt;None&lt;/code&gt;. You are trying to call &lt;code&gt;None.count('1')&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;[!TIP]&lt;br&gt;
&lt;strong&gt;Right&lt;/strong&gt;:&lt;/p&gt;


&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;val_count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;1&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;val_count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  5. String/List Slicing &lt;code&gt;[start:stop]&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The second argument is the &lt;strong&gt;stop index&lt;/strong&gt;, NOT the length.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;[!CAUTION]&lt;br&gt;
&lt;strong&gt;Wrong&lt;/strong&gt;: &lt;code&gt;bin(i)[2:n]&lt;/code&gt; (thinking you want n characters).&lt;/p&gt;

&lt;p&gt;[!TIP]&lt;br&gt;
&lt;strong&gt;Right&lt;/strong&gt;: &lt;code&gt;bin(i)[2:]&lt;/code&gt; (to slice from index 2 to the end).&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  6. Bitwise Operator Precedence
&lt;/h3&gt;

&lt;p&gt;Arithmetic operators (&lt;code&gt;+&lt;/code&gt;, &lt;code&gt;-&lt;/code&gt;, &lt;code&gt;*&lt;/code&gt;, &lt;code&gt;/&lt;/code&gt;) have &lt;strong&gt;higher precedence&lt;/strong&gt; than bitwise operators (&lt;code&gt;&amp;amp;&lt;/code&gt;, &lt;code&gt;|&lt;/code&gt;, &lt;code&gt;^&lt;/code&gt;).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;[!CAUTION]&lt;br&gt;
&lt;strong&gt;Wrong&lt;/strong&gt;: &lt;code&gt;dp[i &amp;gt;&amp;gt; 1] + i &amp;amp; 1&lt;/code&gt;&lt;br&gt;
Evaluated as: &lt;code&gt;(dp[i &amp;gt;&amp;gt; 1] + i) &amp;amp; 1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;[!TIP]&lt;br&gt;
&lt;strong&gt;Right&lt;/strong&gt;: &lt;code&gt;dp[i &amp;gt;&amp;gt; 1] + (i &amp;amp; 1)&lt;/code&gt;&lt;br&gt;
Always use parentheses when mixing arithmetic and bitwise logic.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Math &amp;amp; Numbers
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Modulo
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;  &lt;span class="c1"&gt;# 1 (remainder)
&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="c1"&gt;# 3 (division without remainder)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Floor Division Assignment
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# //= is floor division assignment operator
&lt;/span&gt;&lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;//=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;  &lt;span class="c1"&gt;# Same as: curr = curr // 3
# Result: curr = 3 (integer division, no remainder)
&lt;/span&gt;
&lt;span class="c1"&gt;# Common in sliding window problems
&lt;/span&gt;&lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;//=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  &lt;span class="c1"&gt;# Divide curr by nums[left] using integer division
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Power
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;      &lt;span class="c1"&gt;# 8
&lt;/span&gt;&lt;span class="nf"&gt;pow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# 8
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Python Tip: Arbitrary Precision Integers (The Bit-Depth Cheat Code)
&lt;/h3&gt;

&lt;p&gt;In many languages (Java, C++, Go), integers have a fixed size (usually 32 or 64 bits). If you exceed 2^{63}-1, the number "wraps around" or overflows, leading to negative results and broken logic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In Python, integers have arbitrary precision.&lt;/strong&gt; They will grow to consume as much memory as your computer has.&lt;/p&gt;

&lt;h2&gt;
  
  
  The "Maximum Width" Cheat Code
&lt;/h2&gt;

&lt;p&gt;When solving problems like &lt;strong&gt;Maximum Width of Binary Tree&lt;/strong&gt;, you need to index nodes as 2i, 2i+1. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In a tree with depth 1000, the index would be 2^1000.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Java/C++&lt;/strong&gt;: You must "normalize" the level (subtract the leftmost index) to prevent overflow.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Python&lt;/strong&gt;: You can ignore overflow entirely. Just keep doubling the numbers. Python will handle the 300-digit number without breaking a sweat.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Arbitrary Precision Internals
&lt;/h3&gt;

&lt;p&gt;Python's &lt;code&gt;int&lt;/code&gt; is actually a struct that points to a list of "digits" (usually in base 2^30). As the number gets larger, Python dynamically allocates more "digits" to store it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pitfalls
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Performance&lt;/strong&gt;: While arbitrary precision is convenient, math on 10,000-digit numbers is slower than math on 64-bit hardware-level integers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Memory&lt;/strong&gt;: If you create enough massive integers, you can eventually hit a &lt;code&gt;MemoryError&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The "Normalize" Habit&lt;/strong&gt;: In a real interview, even if you use Python, you should &lt;strong&gt;mention&lt;/strong&gt; the normalization technique. It shows "Senior Signal"—that you understand how lower-level memory works and are aware that your code might not be portable to other languages without it.&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  python #integers #overflow #senior-signal
&lt;/h1&gt;




&lt;h3&gt;
  
  
  Min/Max
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;        &lt;span class="c1"&gt;# 1
&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;      &lt;span class="c1"&gt;# 3
&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# Min by length
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Infinity
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;

&lt;span class="c1"&gt;# Initialize a number to infinity
&lt;/span&gt;&lt;span class="n"&gt;max_pattern_count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;inf&lt;/span&gt;  &lt;span class="c1"&gt;# Positive infinity
&lt;/span&gt;&lt;span class="n"&gt;min_value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;inf&lt;/span&gt;         &lt;span class="c1"&gt;# Negative infinity
&lt;/span&gt;
&lt;span class="c1"&gt;# Common use case: Initialize min to infinity when finding minimum
&lt;/span&gt;&lt;span class="n"&gt;min_val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;inf&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;min_val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;min_val&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Calculate Sum of Digits
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;digit_sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;digit_sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;digit_sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;  &lt;span class="c1"&gt;# Get last digit
&lt;/span&gt;        &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;//=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;             &lt;span class="c1"&gt;# CRITICAL: Use //= not /= (integer division)
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;digit_sum&lt;/span&gt;

&lt;span class="c1"&gt;# Example: digit_sum(123) -&amp;gt; 1 + 2 + 3 = 6
# num % 10 gets the last digit (remainder when dividing by 10)
# num //= 10 removes the last digit (integer division by 10)
&lt;/span&gt;
&lt;span class="c1"&gt;# CRITICAL: Must use //= (integer division), not /= (floating point division)
# WRONG: num /= 10  # This creates float, breaks the loop condition
# CORRECT: num //= 10  # Integer division, removes last digit correctly
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Strings &amp;amp; Characters
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Character Arithmetic: ord() and chr()
&lt;/h3&gt;

&lt;p&gt;Python does not allow direct arithmetic on characters (like &lt;code&gt;char + 1&lt;/code&gt; or &lt;code&gt;char - 'a'&lt;/code&gt;). Instead, you must use the "bridge" functions to convert between characters and their ASCII/Unicode integer values.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bridge Conversion Functions
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;ord(char)&lt;/code&gt;: Character \rightarrow Integer (ASCII code)&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;chr(int)&lt;/code&gt;: Integer \rightarrow Character
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# 1. Increment/Decrement
&lt;/span&gt;&lt;span class="n"&gt;next_char&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;chr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;ord&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# 'b'
&lt;/span&gt;&lt;span class="n"&gt;prev_char&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;chr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;ord&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;z&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# 'y'
&lt;/span&gt;
&lt;span class="c1"&gt;# 2. Get Alphabetical Index (0-25)
#  Mnemonic: "Python makes you say 'ord' out loud"
&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;ord&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;c&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nf"&gt;ord&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# 2
&lt;/span&gt;
&lt;span class="c1"&gt;# 3. Handle Wrap-around (z -&amp;gt; a)
&lt;/span&gt;&lt;span class="n"&gt;char&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;z&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;next_wrapped&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;chr&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nf"&gt;ord&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;char&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nf"&gt;ord&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;26&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;ord&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;# 'a'
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Comparisons across Languages
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Task&lt;/th&gt;
&lt;th&gt;C++ / Java&lt;/th&gt;
&lt;th&gt;Python&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Index&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;c - 'a'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ord(c) - ord('a')&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Shift&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;'a' + 2&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;chr(ord('a') + 2)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  When to use what?
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Use &lt;code&gt;ord/chr&lt;/code&gt;&lt;/strong&gt;: When you need to iterate through the alphabet or treat letters like a numeric range.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Use a lookup string&lt;/strong&gt;: When the alphabet is custom or small (e.g., &lt;code&gt;"ACGT"&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;alphabet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ACGT&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;next_gene&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;alphabet&lt;/span&gt;&lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="n"&gt;alphabet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;A&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# 'C'
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Grid Coordinates: r, c vs. x, y
&lt;/h3&gt;

&lt;p&gt;In grid problems, never use &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; for variables. This is a common trap that leads to "Cartesian Thinking" and swap bugs.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Cartesian Trap (x, y)
&lt;/h3&gt;

&lt;p&gt;In geometry:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;x&lt;/code&gt; = horizontal (left/right) = &lt;strong&gt;Columns&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;y&lt;/code&gt; = vertical (up/down) = &lt;strong&gt;Rows&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In coding:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;grid[x][y]&lt;/code&gt; often leads to people checking &lt;code&gt;0 &amp;lt;= x &amp;lt; rows&lt;/code&gt; but &lt;code&gt;x&lt;/code&gt; should be compared to &lt;code&gt;cols&lt;/code&gt; in standard geometry.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Matrix Standard (r, c)
&lt;/h3&gt;

&lt;p&gt;Always name your variables &lt;code&gt;r&lt;/code&gt; (row) and &lt;code&gt;c&lt;/code&gt; (column) to match the indexing of the matrix: &lt;code&gt;grid[r][c]&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cols&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="c1"&gt;# Move vertically -&amp;gt; row change
&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;dr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dc&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
    &lt;span class="n"&gt;nr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;dr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;dc&lt;/span&gt;

    &lt;span class="c1"&gt;# Logic remains perfectly consistent:
&lt;/span&gt;    &lt;span class="c1"&gt;# nr belongs with rows, nc belongs with cols
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;nr&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;nc&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;cols&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="bp"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Mental Rule
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;r&lt;/code&gt;&lt;/strong&gt; (row) -&amp;gt; &lt;strong&gt;Height&lt;/strong&gt; -&amp;gt; compare with &lt;code&gt;len(grid)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;c&lt;/code&gt;&lt;/strong&gt; (column) -&amp;gt; &lt;strong&gt;Width&lt;/strong&gt; -&amp;gt; compare with &lt;code&gt;len(grid[0])&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By using &lt;code&gt;r&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt;, you physically cannot mix them up.&lt;/p&gt;




&lt;h3&gt;
  
  
  String Methods
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello World&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;           &lt;span class="c1"&gt;# ['Hello', 'World']
&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;l&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;        &lt;span class="c1"&gt;# ['He', '', 'o Wor', 'd']
&lt;/span&gt;&lt;span class="sh"&gt;''&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;# 'ab'
&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;           &lt;span class="c1"&gt;# Remove whitespace
&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;l&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;L&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# 'HeLLo WorLd'
&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startswith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;He&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# True
&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;endswith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ld&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# True
&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;l&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="c1"&gt;# 2 (Handy for counting characters/bits)
&lt;/span&gt;
&lt;span class="c1"&gt;# Convert string to array of characters
&lt;/span&gt;&lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="c1"&gt;# ['h', 'e', 'l', 'l', 'o']
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  String Formatting
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Alice&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;
&lt;span class="c1"&gt;# f-strings (Python 3.6+)
&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;My name is &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; and I&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;m &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Value: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;  &lt;span class="c1"&gt;# Format float to 2 decimals
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Common f-string mistakes
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# WRONG: Using  instead of {} (other languages use )
&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;x=x, y=y&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;  &lt;span class="c1"&gt;# Syntax error!
&lt;/span&gt;
&lt;span class="c1"&gt;# CORRECT: Use curly braces
&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;x=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;, y=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Swapcase for Case-Insensitive Comparison
&lt;/h3&gt;

&lt;p&gt;When you need to check if a character is the same letter as another but with the opposite case (common in "string reduction" or "Great String" problems):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# The "Double Case" Trick
&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;swapcase&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why use it?
&lt;/h3&gt;

&lt;p&gt;Instead of the verbose:&lt;br&gt;
&lt;code&gt;if stack and c.lower() == stack[-1].lower() and c != stack[-1]:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;swapcase()&lt;/code&gt; method handles the "same letter, different case" logic in a single call.&lt;/p&gt;


&lt;h3&gt;
  
  
  Slicing Efficiency in Recursion
&lt;/h3&gt;

&lt;p&gt;When slicing strings in recursive functions (e.g., &lt;code&gt;s[1:]&lt;/code&gt; or &lt;code&gt;s[2:]&lt;/code&gt;), you create a new string copy at every call. This requires &lt;code&gt;O(n)&lt;/code&gt; memory and time on every single recursion stack frame.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Slicing creates a full copy of the trailing string
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;dfs_slow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="bp"&gt;...&lt;/span&gt;
    &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For high performance (like tight LeetCode algorithms with strings of 10,000+ length), pass an expanding index &lt;code&gt;i&lt;/code&gt; instead. Looking up an index is &lt;code&gt;O(1)&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Passing an index guarantees O(1) step performance 
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;dfs_fast&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# Instead of s[1:], we just access i + 1
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;char&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of sending substrings around, keep the raw string intact globally and only send "pointers" defining where you are looking.&lt;/p&gt;




&lt;h3&gt;
  
  
  String Concatenation and Join Errors
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Mistake 1: Concatenating integers with strings&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# WRONG: Can't concatenate int with str using +
&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;x=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;, y=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;  &lt;span class="c1"&gt;# TypeError!
&lt;/span&gt;
&lt;span class="c1"&gt;# CORRECT: Convert to string first or use f-string
&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;x=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;, y=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# OR use f-string (preferred)
&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;x=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;, y=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Mistake 2: Wrong join syntax&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# WRONG: join is a string method, not a list method
&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;c&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;, &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# AttributeError!
&lt;/span&gt;
&lt;span class="c1"&gt;# CORRECT: Call join on the separator string
&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;, &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# 'a, b, c'
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Iteration &amp;amp; Loops
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Python Iterators and Reversing Patterns
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Reversing Methods
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;Returns&lt;/th&gt;
&lt;th&gt;Mutates?&lt;/th&gt;
&lt;th&gt;Best Use Case&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;list(reversed(x))&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;list&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Interviews/LeetCode.&lt;/strong&gt; Explicit, safe, readable.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;x[::-1]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;list&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Short, idiomatic "slicing" shortcut.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;x.reverse()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;None&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Performance-sensitive code where you don't need the original.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;** Watch Out:** &lt;code&gt;reverse(x)&lt;/code&gt; is NOT a thing. Python doesn't have a global reverse function.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Python Iterator Family
&lt;/h3&gt;

&lt;p&gt;Python has a family of built-in functions that don't return lists—they return &lt;strong&gt;Iterators&lt;/strong&gt;. They are "lazy" (they only calculate values as you ask for them).&lt;/p&gt;

&lt;h3&gt;
  
  
  1. &lt;code&gt;reversed(x)&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Returns a &lt;code&gt;list_reverseiterator&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;reversed&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="c1"&gt;# Result: [3, 2, 1] when converted to list
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. &lt;code&gt;enumerate(x)&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Returns pairs of &lt;code&gt;(index, value)&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;enumerate&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]):&lt;/span&gt;
    &lt;span class="c1"&gt;# (0, "a"), (1, "b")
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. &lt;code&gt;zip(a, b)&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Iterates through multiple sequences in parallel.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;zip&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Alice&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Bob&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;]):&lt;/span&gt;
    &lt;span class="c1"&gt;# ("Alice", 10), ("Bob", 20)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. &lt;code&gt;map(fn, x)&lt;/code&gt; &amp;amp; &lt;code&gt;filter(fn, x)&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Transform or filter items lazily.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;squares&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="n"&gt;evens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Crucial Iterator Rules
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Lazy Evaluation&lt;/strong&gt;: No memory is used for the full list until you actually iterate or convert it.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Single-Use&lt;/strong&gt;: Once you loop through an iterator (like &lt;code&gt;reversed(x)&lt;/code&gt;), it is "exhausted." You can't loop through it again.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Not Indexable&lt;/strong&gt;: You cannot do &lt;code&gt;reversed(x)[0]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Materializing&lt;/strong&gt;: If you need a real list (e.g., to return in LeetCode), wrap it: &lt;code&gt;list(reversed(x))&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Summary for Zigzag BFS
&lt;/h2&gt;

&lt;p&gt;When you need to flip a row in a BFS:&lt;br&gt;
&lt;strong&gt;Use &lt;code&gt;list(reversed(row))&lt;/code&gt;&lt;/strong&gt;. It follows the Python iterator model perfectly: safe, explicit, and non-mutating.&lt;/p&gt;


&lt;h3&gt;
  
  
  Enumerate: The Pythonic Way to Track Indices
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;enumerate()&lt;/code&gt; returns an iterable of tuples containing &lt;code&gt;(index, value)&lt;/code&gt;. It is the standard way to loop when you need both the element and its position.&lt;/p&gt;
&lt;h2&gt;
  
  
  1. Basic Syntax
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;apple&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;banana&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;cherry&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;# Standard usage
&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;enumerate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Index &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; has value &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Starting at a different index (e.g., 1-based indexing)
&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;enumerate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Product #&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  2. Common Interview Patterns
&lt;/h2&gt;
&lt;h3&gt;
  
  
  A. Flipping the First Match
&lt;/h3&gt;

&lt;p&gt;Perfect for "Maximum 69 Number" or finding the first occurrence of a target.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;enumerate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;digits&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;6&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;digits&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;9&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  B. Building a Map of Values to Indices
&lt;/h3&gt;

&lt;p&gt;Used for "Two Sum" or tracking last-seen positions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;idx_map&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;enumerate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
&lt;span class="c1"&gt;# Result: {10: 0, 20: 1, 30: 2}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  C. Grid Traversal (Flattened)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;enumerate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;matrix&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cell&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;enumerate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# r = i, c = j
&lt;/span&gt;        &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why Use This?
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Readable&lt;/strong&gt;: &lt;code&gt;for i, x in enumerate(L)&lt;/code&gt; is much cleaner than &lt;code&gt;for i in range(len(L)): x = L[i]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Performance&lt;/strong&gt;: It uses an iterator, which is memory efficient.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Less Error-Prone&lt;/strong&gt;: Prevents "off-by-one" errors common with manual index incrementing.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Tags: #python #basics #clean-code&lt;/p&gt;




&lt;h3&gt;
  
  
  Zip (iterate multiple lists)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;arr1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;arr2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;c&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="c1"&gt;# Iterate both simultaneously
&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;letter&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;zip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arr2&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;letter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Adjacent Pairs (Sliding Window of 2)
&lt;/h2&gt;

&lt;p&gt;You can zip a list with itself, shifted by 1, to elegantly iterate over adjacent pairs without using indices:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;prev_item&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;curr_item&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;zip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:]):&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prev_item&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;curr_item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# Prints:
# 10 20
# 20 30
# 30 40
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Range Off-by-One Errors
&lt;/h3&gt;

&lt;p&gt;The most common bug is missing the last element because &lt;code&gt;range&lt;/code&gt; is &lt;strong&gt;exclusive&lt;/strong&gt; at the end.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Arrays/Lists
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;# WRONG: range(1, len(arr) - 1)
# Stops at index 2. Processed: indices 1, 2. Missing: index 3.
&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="c1"&gt;# CORRECT: range(1, len(arr))
# Processed: indices 1, 2, 3.
&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Inclusive N (0 to N inclusive)
&lt;/h3&gt;

&lt;p&gt;If &lt;code&gt;0 &amp;lt;= i &amp;lt;= n&lt;/code&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;[!CAUTION]&lt;br&gt;
&lt;strong&gt;Wrong&lt;/strong&gt;: &lt;code&gt;range(n)&lt;/code&gt; (stops at &lt;code&gt;n-1&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;[!TIP]&lt;br&gt;
&lt;strong&gt;Right&lt;/strong&gt;: &lt;code&gt;range(n + 1)&lt;/code&gt; (stops at &lt;code&gt;n&lt;/code&gt;)&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Lists &amp;amp; Arrays
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Basic List Comprehension
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Traditional for loop:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;One-liner list comprehension:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  List Comprehension with Condition
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Traditional for loop with if:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;One-liner with condition:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Nested List Comprehension
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Traditional nested loops:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;matrix&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;matrix&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;One-liner nested comprehension:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;matrix&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Flatten Lists
&lt;/h3&gt;

&lt;p&gt;If you have a nested list of lists (e.g., &lt;code&gt;[[1, 2], [3, 4]]&lt;/code&gt;) and need a flat list (&lt;code&gt;[1, 2, 3, 4]&lt;/code&gt;), here are the standard approaches:&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Using a double loop (Most Readable)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;nested_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
&lt;span class="n"&gt;flat_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;sublist&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;nested_list&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;sublist&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;flat_list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Using List Comprehension (Idiomatic)
&lt;/h2&gt;

&lt;p&gt;This is essentially the double loop above, but written in a single line. The &lt;code&gt;for&lt;/code&gt; clauses remain in the exact same left-to-right order as the nested loops.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;flat_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;sublist&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;nested_list&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;sublist&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Using &lt;code&gt;itertools.chain&lt;/code&gt; (Best for large lazy evaluation)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;itertools&lt;/span&gt;
&lt;span class="n"&gt;flat_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;itertools&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;chain&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from_iterable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nested_list&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Iterate Array/String with Index
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Iterate with index using range(len())
&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;  &lt;span class="c1"&gt;# Access index and value
&lt;/span&gt;
&lt;span class="c1"&gt;# Works the same for strings
&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Initialize Array with Same Value
&lt;/h3&gt;

&lt;p&gt;When you need an array of a fixed size initialized with a default value (like zeros for a result array), use the &lt;code&gt;*&lt;/code&gt; operator.&lt;/p&gt;

&lt;h3&gt;
  
  
  Common Use Case: Result Arrays
&lt;/h3&gt;

&lt;p&gt;This is extremely common in problems where you need to return an array of the same length as the input, such as in monotonic stack problems (e.g., "Daily Temperatures").&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Initialize an array of same length as 'temperatures' with zeros
&lt;/span&gt;&lt;span class="n"&gt;answer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;temperatures&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why use this?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Concise&lt;/strong&gt;: &lt;code&gt;[0] * n&lt;/code&gt; is much shorter than a loop or list comprehension.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Performance&lt;/strong&gt;: It is highly optimized in Python.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Standard&lt;/strong&gt;: This is the idiomatic way to pre-allocate a list in Python when the size is known.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The 2D Array Pitfall
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Only use this for primitive types (integers, strings, booleans).&lt;/strong&gt; &lt;br&gt;
To initialize a 2D array, &lt;strong&gt;do not&lt;/strong&gt; use &lt;code&gt;[[0] * cols] * rows&lt;/code&gt;, as this will create multiple references to the &lt;strong&gt;same&lt;/strong&gt; inner list object. &lt;/p&gt;

&lt;p&gt;For 2D arrays, always use a list comprehension:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Correct way for 2D arrays
&lt;/span&gt;&lt;span class="n"&gt;matrix&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;cols&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Initialize Two-Dimensional Array
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# WRONG: Cannot use [][] syntax
&lt;/span&gt;&lt;span class="n"&gt;answer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[][]&lt;/span&gt;  &lt;span class="c1"&gt;# SyntaxError!
&lt;/span&gt;
&lt;span class="c1"&gt;# CORRECT: Initialize as list of lists
&lt;/span&gt;&lt;span class="n"&gt;answer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="n"&gt;answer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;([])&lt;/span&gt;  &lt;span class="c1"&gt;# Append the first empty list
&lt;/span&gt;&lt;span class="n"&gt;answer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;([])&lt;/span&gt;  &lt;span class="c1"&gt;# Append the second empty list
&lt;/span&gt;
&lt;span class="c1"&gt;# OR more simply:
&lt;/span&gt;&lt;span class="n"&gt;answer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[],&lt;/span&gt; &lt;span class="p"&gt;[]]&lt;/span&gt;  &lt;span class="c1"&gt;# List containing two empty lists
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  List Aliasing (Multiple Assignment Pitfall)
&lt;/h3&gt;

&lt;p&gt;In Python, assigning multiple variables to a mutable object in a single line (chained assignment) makes them all point to the &lt;strong&gt;same object&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Pitfall: Chained Assignment
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;cs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;  &lt;span class="c1"&gt;# Both variables point to the same list object!
&lt;/span&gt;
&lt;span class="n"&gt;cs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Output: [1] (Wait, I only modified 'cs'!)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The Correct Way: Separate Initialization
&lt;/h3&gt;

&lt;p&gt;Initialize them individually to create two distinct list objects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;cs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="n"&gt;ts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

&lt;span class="n"&gt;cs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Output: [] (Correct, they are independent)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why this happens?
&lt;/h3&gt;

&lt;p&gt;In Python, &lt;code&gt;=&lt;/code&gt; doesn't copy objects; it creates &lt;strong&gt;references&lt;/strong&gt;. Chained assignment &lt;code&gt;a = b = []&lt;/code&gt; is equivalent to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create an empty list &lt;code&gt;[]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Point &lt;code&gt;b&lt;/code&gt; to that list.&lt;/li&gt;
&lt;li&gt;Point &lt;code&gt;a&lt;/code&gt; to whatever &lt;code&gt;b&lt;/code&gt; is pointing to.
Both are now "aliases" for the same memory location.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Dictionaries &amp;amp; Sets
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Dictionary Comprehension
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Traditional for loop:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;squares&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;squares&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;One-liner dictionary comprehension:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;squares&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Set Comprehension
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Traditional for loop:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;unique_squares&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;unique_squares&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;One-liner set comprehension:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;unique_squares&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Set Initialization &amp;amp; Pitfalls
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Define an empty set
&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# Initialize with values using curly braces (not empty braces - that's a dict!)
&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;# Convert iterable to set
&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;  &lt;span class="c1"&gt;# {1, 2, 3}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Common Pitfalls
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Invalid Multiple Arguments:&lt;/strong&gt;&lt;br&gt;
The &lt;code&gt;set()&lt;/code&gt; constructor takes at most &lt;strong&gt;one&lt;/strong&gt; argument (an iterable).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#  INVALID: set() takes at most 1 argument
&lt;/span&gt;&lt;span class="n"&gt;closing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;]&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 

&lt;span class="c1"&gt;#  CORRECT: Use curly braces
&lt;/span&gt;&lt;span class="n"&gt;closing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;]&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;#  CORRECT: Pass a single string (which is iterable)
&lt;/span&gt;&lt;span class="n"&gt;closing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;)]}&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The String Initialization Pitfall
&lt;/h3&gt;

&lt;p&gt;Be very careful when initializing a set with a single string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="c1"&gt;#  WRONG: Splits string into characters
&lt;/span&gt;&lt;span class="n"&gt;seen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# {'h', 'e', 'l', 'o'}
&lt;/span&gt;
&lt;span class="c1"&gt;#  CORRECT: Use curly braces
&lt;/span&gt;&lt;span class="n"&gt;seen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;# {'hello'}
&lt;/span&gt;
&lt;span class="c1"&gt;#  CORRECT: Wrap in a list
&lt;/span&gt;&lt;span class="n"&gt;seen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;# {'hello'}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Hashmap/Dictionary Operations
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Declaration: a hash map is declared like any other variable. The syntax is {}
&lt;/span&gt;&lt;span class="n"&gt;hash_map&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="c1"&gt;# If you want to initialize it with some key value pairs, use the following syntax:
&lt;/span&gt;&lt;span class="n"&gt;hash_map&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;# Checking if a key exists: simply use the `in` keyword
&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;hash_map&lt;/span&gt;  &lt;span class="c1"&gt;# True
&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;hash_map&lt;/span&gt;  &lt;span class="c1"&gt;# False
&lt;/span&gt;
&lt;span class="c1"&gt;# Accessing a value given a key: use square brackets, similar to an array.
&lt;/span&gt;&lt;span class="n"&gt;hash_map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  &lt;span class="c1"&gt;# 3
&lt;/span&gt;
&lt;span class="c1"&gt;# Adding or updating a key: use square brackets, similar to an array.
# If the key already exists, the value will be updated
&lt;/span&gt;&lt;span class="n"&gt;hash_map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;

&lt;span class="c1"&gt;# If the key doesn't exist yet, the key value pair will be inserted
&lt;/span&gt;&lt;span class="n"&gt;hash_map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;

&lt;span class="c1"&gt;# Deleting a key: use the del keyword. Key must exist or you will get an error.
&lt;/span&gt;&lt;span class="k"&gt;del&lt;/span&gt; &lt;span class="n"&gt;hash_map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;# Get size
&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hash_map&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# 3
&lt;/span&gt;
&lt;span class="c1"&gt;# Get keys: use .keys(). You can iterate over this using a for loop.
&lt;/span&gt;&lt;span class="n"&gt;keys&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hash_map&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Iterating directly over dictionary iterates over keys
&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;hash_map&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;# Same as for key in hash_map.keys()
&lt;/span&gt;    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Get values: use .values(). You can iterate over this using a for loop.
&lt;/span&gt;&lt;span class="n"&gt;values&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hash_map&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;values&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Convert values to a list
&lt;/span&gt;&lt;span class="n"&gt;values_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hash_map&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;values&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;  &lt;span class="c1"&gt;# [2, 3, 2]
&lt;/span&gt;
&lt;span class="c1"&gt;# Iterate over both keys and values using .items()
&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;hash_map&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;items&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Prints both key and value
&lt;/span&gt;
&lt;span class="c1"&gt;# Common pattern in coding problems
&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;summed_map&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;items&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="c1"&gt;# Process both key and value together
&lt;/span&gt;    &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Counter (from collections)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;collections&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Counter&lt;/span&gt;

&lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# count[1] = 1, count[2] = 2, count[3] = 3
# count.most_common(2) returns [(3, 3), (2, 2)]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Check if All Occurrences Are Equal (One-liner)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;collections&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Counter&lt;/span&gt;

&lt;span class="c1"&gt;# Check if all character occurrences in a string are equal
&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;aabbcc&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="c1"&gt;# Counter(s).values() gives all counts, set() removes duplicates
# If all counts are equal, set will have length 1
&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;values&lt;/span&gt;&lt;span class="p"&gt;()))&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;  &lt;span class="c1"&gt;# True if all chars appear same number of times
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Defaultdict (from collections)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;collections&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;defaultdict&lt;/span&gt;

&lt;span class="c1"&gt;# No need to check if key exists
&lt;/span&gt;&lt;span class="n"&gt;dd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;defaultdict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;dd&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;key&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;  &lt;span class="c1"&gt;# Works even if 'key' doesn't exist
&lt;/span&gt;
&lt;span class="n"&gt;dd_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;defaultdict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;dd_list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;key&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Automatically creates list
&lt;/span&gt;
&lt;span class="c1"&gt;# Common pattern: Group items by a key
&lt;/span&gt;&lt;span class="n"&gt;summed_map&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;defaultdict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;summed_map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;digits_sum&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Adding item to list in value of map
# If digits_sum key doesn't exist, defaultdict automatically creates empty list
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Hashability Pitfall (List vs. Tuple)
&lt;/h3&gt;

&lt;h3&gt;
  
  
  The Pitfall: Adding a List to a Set/Dict
&lt;/h3&gt;

&lt;p&gt;In Python, &lt;strong&gt;mutable&lt;/strong&gt; objects like lists, sets, and dictionaries cannot be used as keys in a dictionary or elements in a set.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; 
&lt;span class="c1"&gt;#  TypeError: unhashable type: 'list'
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The Fix: Convert to Tuple
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Tuples&lt;/strong&gt; are immutable and therefore hashable. Convert your list to a tuple before adding it to a set or using it as a dictionary key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;#  Works!
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why this matters?
&lt;/h3&gt;

&lt;p&gt;To provide O(1) lookup, sets and dictionaries use a hash function to calculate the object's "fingerprint." If the object is mutable (like a list), its contents could change, which would change its hash and break the data structure's internal mapping.&lt;/p&gt;

&lt;h3&gt;
  
  
  Common Interview Scenario
&lt;/h3&gt;

&lt;p&gt;In problems like &lt;strong&gt;3Sum&lt;/strong&gt; or &lt;strong&gt;Group Anagrams&lt;/strong&gt;, you often need to store a "triplet" or a "frequency signature." Always use a &lt;strong&gt;tuple&lt;/strong&gt; for these cases.&lt;/p&gt;




&lt;h2&gt;
  
  
  Queues &amp;amp; Stacks
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Queue Operations (using deque)
&lt;/h3&gt;

&lt;p&gt;In Python, &lt;code&gt;collections.deque&lt;/code&gt; is the standard way to implement a queue because it provides O(1) time complexity for both append and pop operations from both ends.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;collections&lt;/span&gt;

&lt;span class="c1"&gt;# Declaration: we will use deque from the collections module
&lt;/span&gt;&lt;span class="n"&gt;queue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;collections&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deque&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# If you want to initialize it with some initial values:
&lt;/span&gt;&lt;span class="n"&gt;queue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;collections&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deque&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="c1"&gt;# Enqueueing/adding elements:
&lt;/span&gt;&lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Dequeuing/removing elements:
&lt;/span&gt;&lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;popleft&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# Returns 1
&lt;/span&gt;&lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;popleft&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# Returns 2
&lt;/span&gt;
&lt;span class="c1"&gt;# Check element at front of queue (next element to be removed)
&lt;/span&gt;&lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# 3
&lt;/span&gt;
&lt;span class="c1"&gt;# Get size
&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# 3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why not use a list?
&lt;/h3&gt;

&lt;p&gt;While you can use &lt;code&gt;list.pop(0)&lt;/code&gt;, it is an O(n) operation because all other elements have to be shifted. &lt;code&gt;deque.popleft()&lt;/code&gt; is O(1).&lt;/p&gt;

&lt;h3&gt;
  
  
  The pop(0) Trap
&lt;/h3&gt;

&lt;p&gt;A common mistake is trying to call &lt;code&gt;queue.pop(0)&lt;/code&gt; on a &lt;code&gt;deque&lt;/code&gt; object. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;list.pop(index)&lt;/code&gt; accepts an index.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;deque.pop()&lt;/code&gt; &lt;strong&gt;takes no arguments&lt;/strong&gt; and only pops from the RIGHT.&lt;/li&gt;
&lt;li&gt;If you want the left, you MUST use &lt;code&gt;popleft()&lt;/code&gt;. Calling &lt;code&gt;deque.pop(0)&lt;/code&gt; will raise a &lt;code&gt;TypeError&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  List as Stack
&lt;/h3&gt;

&lt;p&gt;In Python, a standard list is the most common way to implement a stack.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conceptual Clarification
&lt;/h3&gt;

&lt;p&gt;Think of a list-stack like a &lt;strong&gt;stack of plates&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;The "Top" of the stack is the LAST element of the list.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;append()&lt;/code&gt; adds a plate to the &lt;strong&gt;top&lt;/strong&gt; (the end of the list).&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;pop()&lt;/code&gt; removes a plate from the &lt;strong&gt;top&lt;/strong&gt; (the end of the list).&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Crucial:&lt;/strong&gt; We never touch the beginning (index 0) because shifting elements is O(n), while working at the end is O(1).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Declaration
&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

&lt;span class="c1"&gt;# Pushing elements: O(1)
# Adds to the END of the list (the "Top")
&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# [1]
&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# [1, 2]
&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# [1, 2, 3] &amp;lt;-- 3 is the top
&lt;/span&gt;
&lt;span class="c1"&gt;# Popping elements: O(1)
# Removes from the END of the list
&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# Returns 3 (now stack is [1, 2])
&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# Returns 2 (now stack is [1])
&lt;/span&gt;
&lt;span class="c1"&gt;# Check element at top (Peek)
# Always use [-1] for the top element
&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# 1
&lt;/span&gt;
&lt;span class="c1"&gt;# Check if empty (True if empty, False otherwise)
&lt;/span&gt;&lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;   &lt;span class="c1"&gt;# False
&lt;/span&gt;
&lt;span class="c1"&gt;# Get size
&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# 1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  When to Use a Stack
&lt;/h3&gt;

&lt;p&gt;Beyond the obvious LIFO property, a stack is a powerful tool whenever elements in the input &lt;strong&gt;interact with each other&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Recognition Patterns
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;LIFO Interaction:&lt;/strong&gt; Elements need to be matched or compared with the most recent element seen.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Matching Elements:&lt;/strong&gt; Classic examples include valid parentheses or matching opening/closing tags.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Property Queries:&lt;/strong&gt; Finding the "next largest element" or "next smallest element" (Monotonic Stacks).&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Expression Evaluation:&lt;/strong&gt; Mathematical equations provided as strings, where operator precedence or nested sub-expressions require temporary storage.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Abstract Comparison:&lt;/strong&gt; Any problem where you need to compare the current element against a "history" that changes as you process the input.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; If the LIFO property is hard to see, ask yourself: "Does the current element need to interact with the most recently stored element?" If yes, a stack is likely the answer.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Atomic Deque Pop &amp;amp; Update Trick
&lt;/h3&gt;

&lt;p&gt;When maintaining a running total or count while removing elements from a queue (common in sliding windows or streams), you can use the return value of &lt;code&gt;popleft()&lt;/code&gt; directly in an expression.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# The "Double Action" Trick
# No need to peek with queue[0] first!
&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;running_sum&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;popleft&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Benefits of Monotonic Stacks
&lt;/h3&gt;

&lt;p&gt;It combines two operations into one line:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Extracts&lt;/strong&gt; the value being removed.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Removes&lt;/strong&gt; the element from the deque.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Instead of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;running_sum&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt;
&lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;popleft&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;running_sum&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;popleft&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is cleaner and prevents bugs where you might subtract the wrong element if you're not careful with the order of operations.&lt;/p&gt;




&lt;h3&gt;
  
  
  Monotonic Decreasing Stack (Storing Indices)
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;Monotonic Decreasing Stack&lt;/strong&gt; is a powerful pattern used to find the &lt;strong&gt;Next Greater Element&lt;/strong&gt;. By keeping the stack sorted in decreasing order, we "hold onto" values until we encounter a larger one that "resolves" them.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Index Pattern
&lt;/h3&gt;

&lt;p&gt;In many problems, you should store &lt;strong&gt;indices&lt;/strong&gt; in the stack instead of values. This is crucial because indices allow you to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Access the value:&lt;/strong&gt; &lt;code&gt;array[stack[-1]]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Calculate distance:&lt;/strong&gt; &lt;code&gt;current_index - popped_index&lt;/code&gt; (as seen in "Daily Temperatures").&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Implementation Template
&lt;/h3&gt;

&lt;p&gt;When you see a higher value, you pop from the stack until the decreasing invariant is restored.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;dailyTemperatures&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;temperatures&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
        &lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;  &lt;span class="c1"&gt;# Stores ONLY indices
&lt;/span&gt;        &lt;span class="n"&gt;answer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;temperatures&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;temperatures&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
            &lt;span class="c1"&gt;# While the current temp is HIGHER than the temp at the top of the stack
&lt;/span&gt;            &lt;span class="c1"&gt;# It means we've found the "next warmer day" for the item at the top.
&lt;/span&gt;            &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;temperatures&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;temperatures&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
                &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                &lt;span class="n"&gt;answer&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;  &lt;span class="c1"&gt;# Calculate the distance
&lt;/span&gt;
            &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Push current index to maintain the decreasing stack
&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;answer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Mental Model
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Decreasing Stack:&lt;/strong&gt; "I'm waiting for someone bigger than me."&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;The &lt;code&gt;while&lt;/code&gt; loop:&lt;/strong&gt; "Now that I've found someone bigger, I can calculate my result and leave the stack."&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Time Complexity:&lt;/strong&gt; O(n) because each element is pushed and popped exactly once.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Heaps
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Heap Operations (heapq)
&lt;/h3&gt;

&lt;p&gt;In Python, the &lt;code&gt;heapq&lt;/code&gt; module provides an implementation of the heap queue algorithm, also known as the priority queue algorithm. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: &lt;code&gt;heapq&lt;/code&gt; only implements min heaps.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;heapq&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;

&lt;span class="c1"&gt;# Declaration: heapq does not give you a heap data structure.
# You just use a normal list, and heapq provides you with
# methods that can be used on this list to perform heap operations
&lt;/span&gt;&lt;span class="n"&gt;heap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

&lt;span class="c1"&gt;# Add to heap
&lt;/span&gt;&lt;span class="nf"&gt;heappush&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;heappush&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;heappush&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Check minimum element (O(1))
&lt;/span&gt;&lt;span class="n"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  &lt;span class="c1"&gt;# 1
&lt;/span&gt;
&lt;span class="c1"&gt;# Pop minimum element (O(log n))
&lt;/span&gt;&lt;span class="nf"&gt;heappop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# 1
&lt;/span&gt;
&lt;span class="c1"&gt;# Get size
&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# 2
&lt;/span&gt;
&lt;span class="c1"&gt;# Bonus: convert a list to a heap in linear time (O(n))
&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;43&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;634&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;120&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="c1"&gt;#  PITFALL: heapify is IN-PLACE and returns None
&lt;/span&gt;&lt;span class="nf"&gt;heapify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="c1"&gt;# Now 'nums' is a valid heap. Do NOT do: nums = heapify(nums)
&lt;/span&gt;
&lt;span class="c1"&gt;# Now, you can use heappush and heappop on nums
# and nums[0] will always be the minimum element
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Max Heap in Python
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Python 3.14+ (Native Support)
&lt;/h4&gt;

&lt;p&gt;As of Python 3.14, &lt;code&gt;heapq&lt;/code&gt; provides native public functions for max-heaps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;heapify_max(list)&lt;/code&gt;: &lt;strong&gt;In-place&lt;/strong&gt; transformation.&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;heappush_max(heap, item)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;heappop_max(heap)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;heapreplace_max(heap, item)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Pre-Python 3.14 (The Negation Trick)
&lt;/h4&gt;

&lt;p&gt;Since &lt;code&gt;heapq&lt;/code&gt; was historically a MIN heap only, use negative values to simulate a Max Heap:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;max_heap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="nf"&gt;heappush&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_heap&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;heappush&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_heap&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Pop the largest
&lt;/span&gt;&lt;span class="n"&gt;largest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nf"&gt;heappop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_heap&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# 20
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  The "Kth Largest" Pattern (The Best Interview Answer)
&lt;/h3&gt;

&lt;p&gt;Don't use a Max-Heap for finding the Kth &lt;em&gt;largest&lt;/em&gt; element unless you have to. Use a &lt;strong&gt;Min-Heap of size K&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Strategy (The "Bouncer" Logic):&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Maintain a min-heap of size K.&lt;/li&gt;
&lt;li&gt;The root (&lt;code&gt;heap[0]&lt;/code&gt;) is the "shortest person in the club."&lt;/li&gt;
&lt;li&gt;If a new number is larger than the root, kick the root out and let the new number in.&lt;/li&gt;
&lt;li&gt;After processing everything, the root is the Kth largest.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Complexity:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time:&lt;/strong&gt; O(N \log K) — Better than O(N \log N) if k \ll N.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space:&lt;/strong&gt; O(K) — Better than O(N) for streaming data.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;findKthLargest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;heap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;heapq&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;heapify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# O(K)
&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
            &lt;span class="n"&gt;heapq&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;heapreplace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;# O(log K)
&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Common Heap Pitfalls
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Mixing Logic&lt;/strong&gt;: Never use &lt;code&gt;heappop()&lt;/code&gt; on a heap created with &lt;code&gt;heapify_max()&lt;/code&gt; (pre-3.14). Standard &lt;code&gt;heappop&lt;/code&gt; uses Min-Heap logic and will corrupt your Max-Heap structure.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;heapify&lt;/code&gt; In-Place&lt;/strong&gt;: Remember &lt;code&gt;n = heapify(nums)&lt;/code&gt; sets &lt;code&gt;n&lt;/code&gt; to &lt;code&gt;None&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Index Access&lt;/strong&gt;: Only &lt;code&gt;heap[0]&lt;/code&gt; is guaranteed. &lt;code&gt;heap[-1]&lt;/code&gt; is &lt;strong&gt;not&lt;/strong&gt; the maximum/minimum.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Tags: #python #heap #priority-queue #data-structures #complexity #top-k&lt;/p&gt;




&lt;h2&gt;
  
  
  Linked Lists
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Linked List Insertion Logic
&lt;/h3&gt;

&lt;p&gt;When inserting a new node into a single linked list, the order of operations is critical. It can be counter-intuitive because you must update two &lt;code&gt;.next&lt;/code&gt; pointers in a specific order to avoid losing the rest of the list.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementation
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ListNode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;

&lt;span class="c1"&gt;# Let prev_node be the node at position i - 1
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add_node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prev_node&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;node_to_add&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# 1. First, point the new node to the rest of the list
&lt;/span&gt;    &lt;span class="n"&gt;node_to_add&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;prev_node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;

    &lt;span class="c1"&gt;# 2. Then, point the previous node to the new node
&lt;/span&gt;    &lt;span class="n"&gt;prev_node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;node_to_add&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Rationale for Interval Sorting Orders
&lt;/h3&gt;

&lt;p&gt;Think of it as &lt;strong&gt;"securing the rest of the list first"&lt;/strong&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Step 1 (&lt;code&gt;node_to_add.next = prev_node.next&lt;/code&gt;)&lt;/strong&gt;: You first connect your new node to the "tail" of the list (the part that comes after the insertion point). This ensures you have a pointer to the rest of the list.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Step 2 (&lt;code&gt;prev_node.next = node_to_add&lt;/code&gt;)&lt;/strong&gt;: Once the rest of the list is safely "held" by the new node, you can safely update the &lt;code&gt;prev_node&lt;/code&gt; to point to the new node.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;The Pitfall:&lt;/strong&gt; If you reversed these steps, you would point &lt;code&gt;prev_node&lt;/code&gt; to the new node &lt;em&gt;first&lt;/em&gt;. But then you would lose the reference to the original &lt;code&gt;prev_node.next&lt;/code&gt;, making it impossible to connect your new node to the rest of the list (the rest of the list becomes "orphaned").&lt;/p&gt;




&lt;h3&gt;
  
  
  Linked Lists with Sentinel Nodes
&lt;/h3&gt;

&lt;p&gt;Sentinel nodes (dummy nodes) simplify linked list operations by eliminating edge cases like empty lists or deleting the last node.&lt;/p&gt;

&lt;h3&gt;
  
  
  Linked List Sentinel Core Concept
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Head Sentinel:&lt;/strong&gt; &lt;code&gt;head.next&lt;/code&gt; points to the first "real" node.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tail Sentinel:&lt;/strong&gt; &lt;code&gt;tail.prev&lt;/code&gt; points to the last "real" node.&lt;/li&gt;
&lt;li&gt;Operations are always O(1) when adding/removing from both ends.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Implementation
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ListNode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;

&lt;span class="c1"&gt;# Initialization
&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ListNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nc"&gt;ListNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add_to_start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;
    &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;
    &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;remove_from_start&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="c1"&gt;# Empty list
&lt;/span&gt;    &lt;span class="n"&gt;to_remove&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;
    &lt;span class="n"&gt;to_remove&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;
    &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;to_remove&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Rationale for Sentinel Nodes
&lt;/h3&gt;

&lt;p&gt;Without sentinels, you'd need &lt;code&gt;if node.next is None&lt;/code&gt; checks everywhere. With sentinels, every "real" node is guaranteed to have a neighbor, so &lt;code&gt;node.next.prev&lt;/code&gt; or &lt;code&gt;node.prev.next&lt;/code&gt; never fails.&lt;/p&gt;




&lt;h3&gt;
  
  
  Middle of Linked List (Manual Counting)
&lt;/h3&gt;

&lt;p&gt;If you prefer counting nodes/steps manually instead of using Fast and Slow pointers, you can avoid messy &lt;code&gt;if&lt;/code&gt; statements for odd/even lengths by using a specific integer division pattern.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step Counting Pattern
&lt;/h3&gt;

&lt;p&gt;If you count the number of &lt;strong&gt;jumps&lt;/strong&gt; (edges) rather than nodes, you have to handle the parity manually unless you use this formula:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;middleNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Optional&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;ListNode&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Optional&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;ListNode&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
        &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
        &lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;

        &lt;span class="c1"&gt;# Count the "steps" (number of next pointers)
&lt;/span&gt;        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
            &lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;

        &lt;span class="c1"&gt;# The 'if count % 2 == 0' can be replaced with (count + 1) // 2
&lt;/span&gt;        &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;

        &lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;curr&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Deriving the Middle-Node Formula &lt;code&gt;(count + 1) // 2&lt;/code&gt;
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Nodes (length)&lt;/th&gt;
&lt;th&gt;Jumps (&lt;code&gt;count&lt;/code&gt;)&lt;/th&gt;
&lt;th&gt;Resulting &lt;code&gt;mid&lt;/code&gt;
&lt;/th&gt;
&lt;th&gt;Target Index&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;5&lt;/strong&gt; (Odd)&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(4 + 1) // 2 = 2&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;2 (Node 3)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;6&lt;/strong&gt; (Even)&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(5 + 1) // 2 = 3&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;3 (Node 4)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This pattern ensures you always hit the "second middle" node for even-length lists as required by most LeetCode-style problems, without needing an explicit &lt;code&gt;if&lt;/code&gt; check.&lt;/p&gt;




&lt;h3&gt;
  
  
  Linked List Reversal Pitfalls (Reverse Sub-list)
&lt;/h3&gt;

&lt;p&gt;When reversing a sub-segment of a linked list (e.g., &lt;code&gt;Reverse Linked List II&lt;/code&gt;), there are five critical pitfalls to watch out for.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The "Vanishing Head"
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Pitfall:&lt;/strong&gt; Returning the original &lt;code&gt;head&lt;/code&gt; pointer.&lt;br&gt;
&lt;strong&gt;Why:&lt;/strong&gt; If &lt;code&gt;left = 1&lt;/code&gt;, the first node moves, and &lt;code&gt;head&lt;/code&gt; is no longer the start of the list.&lt;br&gt;
&lt;strong&gt;Fix:&lt;/strong&gt; Always use a &lt;code&gt;dummy&lt;/code&gt; node (&lt;code&gt;dummy = ListNode(0, head)&lt;/code&gt;) and return &lt;code&gt;dummy.next&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  2. Reconnection Confusion
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Pitfall:&lt;/strong&gt; Connecting &lt;code&gt;lag.next&lt;/code&gt; to the wrong node or creating a cycle.&lt;br&gt;
&lt;strong&gt;Why:&lt;/strong&gt; After the loop, &lt;code&gt;prev&lt;/code&gt; is the new head of the sub-segment, and &lt;code&gt;cur&lt;/code&gt; is the start of the remaining list.&lt;br&gt;
&lt;strong&gt;Fix:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;lag.next = prev&lt;/code&gt; (Connect the node before the reversal to the new head).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;left_node.next = cur&lt;/code&gt; (Connect the original start of the sub-segment to the rest of the list).&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  3. Pointer Ending Positions
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Pitfall:&lt;/strong&gt; Assuming &lt;code&gt;cur&lt;/code&gt; is the last node of the reversed segment.&lt;br&gt;
&lt;strong&gt;Why:&lt;/strong&gt; The &lt;code&gt;for&lt;/code&gt; loop logic or &lt;code&gt;while cur&lt;/code&gt; logic usually pushes &lt;code&gt;cur&lt;/code&gt; one step &lt;strong&gt;past&lt;/strong&gt; the segment being processed.&lt;br&gt;
&lt;strong&gt;Fix:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;prev&lt;/code&gt; is on the &lt;strong&gt;last&lt;/strong&gt; processed node (the new head).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cur&lt;/code&gt; is on the &lt;strong&gt;next&lt;/strong&gt; unprocessed node (the tail's successor).&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  4. The &lt;code&gt;lag&lt;/code&gt; Initialization
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Pitfall:&lt;/strong&gt; Initializing &lt;code&gt;lag&lt;/code&gt; at &lt;code&gt;head&lt;/code&gt;.&lt;br&gt;
&lt;strong&gt;Why:&lt;/strong&gt; If &lt;code&gt;left = 1&lt;/code&gt;, your &lt;code&gt;while index &amp;lt; left&lt;/code&gt; loop won't run, and &lt;code&gt;lag&lt;/code&gt; remains at &lt;code&gt;head&lt;/code&gt;. This breaks the connection logic.&lt;br&gt;
&lt;strong&gt;Fix:&lt;/strong&gt; Initialize &lt;code&gt;lag = dummy&lt;/code&gt;. This ensures &lt;code&gt;lag&lt;/code&gt; is always exactly one node before the reversal start.&lt;/p&gt;
&lt;h2&gt;
  
  
  5. Variable Scope &amp;amp; Off-by-Ones
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Pitfall:&lt;/strong&gt; Using a temporary variable like &lt;code&gt;after&lt;/code&gt; outside the loop.&lt;br&gt;
&lt;strong&gt;Why:&lt;/strong&gt; If the range is small or empty, &lt;code&gt;after&lt;/code&gt; might not be defined or might point to an old state.&lt;br&gt;
&lt;strong&gt;Fix:&lt;/strong&gt; Use &lt;code&gt;cur&lt;/code&gt; for reconnection instead of the temporary &lt;code&gt;after&lt;/code&gt; variable used inside the loop.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;Quiz Question Ideas:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If reversing nodes 2 to 4, where does &lt;code&gt;prev&lt;/code&gt; sit after the loop?&lt;/li&gt;
&lt;li&gt;Why is &lt;code&gt;lag = dummy&lt;/code&gt; safer than &lt;code&gt;lag = head&lt;/code&gt;?&lt;/li&gt;
&lt;li&gt;What happens if you return &lt;code&gt;head&lt;/code&gt; when &lt;code&gt;left = 1&lt;/code&gt;?&lt;/li&gt;
&lt;/ol&gt;


&lt;h2&gt;
  
  
  Trees &amp;amp; Graphs
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Recursive Reattachment Pattern
&lt;/h3&gt;

&lt;p&gt;This is one of the most critical patterns for Tree problems where you &lt;strong&gt;modify&lt;/strong&gt; the tree structure.&lt;/p&gt;
&lt;h3&gt;
  
  
  Silent Reattachment Failures
&lt;/h3&gt;

&lt;p&gt;In Python, if you pass &lt;code&gt;root.left&lt;/code&gt; to a function, you are passing the &lt;strong&gt;object&lt;/strong&gt; it points to. If that function returns a new node, &lt;code&gt;root.left&lt;/code&gt; doesn't magically update to point to it.&lt;/p&gt;
&lt;h3&gt;
  
  
  Return and Catch
&lt;/h3&gt;

&lt;p&gt;Every recursive call must return the "root" of its subtree, and the caller must "catch" it and assign it to the correct pointer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;modifyTree&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;NewNode&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# 1. CREATE
&lt;/span&gt;
    &lt;span class="c1"&gt;# 2. ASSIGN / CONNECT
&lt;/span&gt;    &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;modifyTree&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;modifyTree&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# 3. PROPAGATE
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Common Tree Reattachment PitfallsMost of the time, &lt;code&gt;modifyTree(root.left)&lt;/code&gt; returns the exact same node that was already there. It feels like you are doing redundant work by re-assigning &lt;code&gt;root.left = root.left&lt;/code&gt;.
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;However&lt;/strong&gt;, at the internal leaf/insertion point:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;modifyTree(None)&lt;/code&gt; returns a brand new &lt;code&gt;TreeNode&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The caller (the parent) does &lt;code&gt;root.left = [New Node]&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;This is the &lt;strong&gt;only&lt;/strong&gt; time the pointer actually changes!&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Where you'll use this
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;BST Insert&lt;/strong&gt;: &lt;code&gt;root.left = insert(root.left, val)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;BST Delete&lt;/strong&gt;: &lt;code&gt;root.left = delete(root.left, val)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Invert Binary Tree&lt;/strong&gt;: &lt;code&gt;root.left, root.right = invert(root.right), invert(root.left)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pruning&lt;/strong&gt;: &lt;code&gt;root.left = prune(root.left)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Tree Recursion Checklist
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;[ ] Does my base case return a node?&lt;/li&gt;
&lt;li&gt;[ ] Am I assigning the result of the recursive call to &lt;code&gt;root.left&lt;/code&gt; or &lt;code&gt;root.right&lt;/code&gt;?&lt;/li&gt;
&lt;li&gt;[ ] Am I returning &lt;code&gt;root&lt;/code&gt; at the end of the function?&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Rule of Thumb&lt;/strong&gt;: If you are changing where a pointer points, you probably need &lt;code&gt;root.left = recurse(...)&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  BFS Pattern: Layer-by-Layer Traversal
&lt;/h3&gt;

&lt;p&gt;Most &lt;strong&gt;Breadth-First Search (BFS)&lt;/strong&gt; implementations use a &lt;code&gt;deque&lt;/code&gt; from &lt;code&gt;collections&lt;/code&gt; to achieve O(1) &lt;code&gt;popleft()&lt;/code&gt; operations. The "Level-by-Level" variation is critical for problems requiring distance or layer processing.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Template
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;collections&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;deque&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;bfs_traversal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;

    &lt;span class="n"&gt;queue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;deque&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# 1. Capture the exact number of nodes in the CURRENT layer
&lt;/span&gt;        &lt;span class="n"&gt;nodes_in_current_level&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="c1"&gt;# [Optional] Logic that happens once per level (e.g., depth tracking)
&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nodes_in_current_level&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;popleft&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

            &lt;span class="c1"&gt;# 2. Logic for the INDIVIDUAL node
&lt;/span&gt;            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

            &lt;span class="c1"&gt;# 3. Queue up the NEXT level
&lt;/span&gt;            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why &lt;code&gt;len(queue)&lt;/code&gt; inside the &lt;code&gt;while&lt;/code&gt; loop?
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;for _ in range(nodes_in_current_level)&lt;/code&gt; loop ensures that you process precisely one "generation" of nodes at a time. Without this, you wouldn't be able to distinguish between levels, which is required for problems like &lt;strong&gt;Level Order Traversal&lt;/strong&gt; or &lt;strong&gt;Right Side View&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  When to use
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Shortest Path&lt;/strong&gt; in an unweighted graph/grid.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Level Order Traversal&lt;/strong&gt; (Binary Tree).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multi-source BFS&lt;/strong&gt; (e.g., Rotting Oranges).&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  DFS Pattern: Iterative Stack Traversal
&lt;/h3&gt;

&lt;p&gt;For &lt;strong&gt;Depth-First Search (DFS)&lt;/strong&gt;, while recursion is common, an &lt;strong&gt;iterative&lt;/strong&gt; approach using a stack is often safer for very deep trees (avoiding &lt;code&gt;RecursionError&lt;/code&gt;) and is a standard interview pattern.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Template
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;dfs_iterative&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;

    &lt;span class="c1"&gt;# 1. Initialize the stack with an array containing the root
&lt;/span&gt;    &lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# 2. Pop the LATEST added node (LIFO)
&lt;/span&gt;        &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

        &lt;span class="c1"&gt;# 3. Process the node
&lt;/span&gt;        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="c1"&gt;# 4. Push children onto the stack
&lt;/span&gt;        &lt;span class="c1"&gt;# To maintain the same order as recursive DFS (Left then Right),
&lt;/span&gt;        &lt;span class="c1"&gt;# we push Right THEN Left because it's a stack.
&lt;/span&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Key Differences from BFS
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Data Structure&lt;/strong&gt;: Uses a standard Python list &lt;code&gt;[]&lt;/code&gt; as a Stack (O(1) &lt;code&gt;pop()&lt;/code&gt;) instead of a &lt;code&gt;deque&lt;/code&gt; (O(1) &lt;code&gt;popleft()&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Order of Children&lt;/strong&gt;: We push children in &lt;strong&gt;reverse order&lt;/strong&gt; (Right then Left) if we want the Left child to be the first one popped and processed next.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Initialization&lt;/strong&gt;: Just like BFS, we initialize the structure with &lt;code&gt;[root]&lt;/code&gt;, but the behavior changes entirely based on whether we use &lt;code&gt;pop()&lt;/code&gt; or &lt;code&gt;popleft()&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  When to use
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Preorder Traversal&lt;/strong&gt; (Iterative).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Path-finding&lt;/strong&gt; where you want to go as deep as possible before backtracking.&lt;/li&gt;
&lt;li&gt;When you want to avoid recursion limits.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  BFS Invariant: Cleaning up Layer-by-Layer Logic
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Counting Traps
&lt;/h3&gt;

&lt;p&gt;Many BFS implementations over-complicate things by manually tracking levels, using multiple passes, or duplicating logic.&lt;/p&gt;

&lt;h3&gt;
  
  
  Node Counting Logic
&lt;/h3&gt;

&lt;p&gt;In any layer-by-layer BFS (using &lt;code&gt;len(q)&lt;/code&gt;), &lt;strong&gt;the last level you process is the deepest level&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Instead of tracking &lt;code&gt;max_level&lt;/code&gt; or running a first pass to find depth:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Initialize &lt;code&gt;ans = 0&lt;/code&gt; (or &lt;code&gt;level_sum&lt;/code&gt;) inside the &lt;code&gt;while q:&lt;/code&gt; loop but &lt;em&gt;outside&lt;/em&gt; the &lt;code&gt;for _ in range(len(q)):&lt;/code&gt; loop.&lt;/li&gt;
&lt;li&gt; Process the level.&lt;/li&gt;
&lt;li&gt; When the queue is empty, the &lt;code&gt;ans&lt;/code&gt; from the &lt;strong&gt;last&lt;/strong&gt; finished iteration is your result.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  BFS Solution (Cleanest Canonical Version)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;collections&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;deque&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;deepest_leaves_sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

    &lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;deque&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;level_sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;  &lt;span class="c1"&gt;# &amp;lt;--- Reset for EVERY level
&lt;/span&gt;        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
            &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;popleft&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="n"&gt;level_sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# When loop finishes, level_sum holds the LAST level's total
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;level_sum&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Layer-by-Layer Invariant&amp;gt; &lt;strong&gt;BFS invariant&lt;/strong&gt;:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;“Each loop iteration = one tree level”&lt;br&gt;
“The last computed level sum = deepest leaves sum”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Benefits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Single traversal&lt;/strong&gt;: O(N)&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Minimal state&lt;/strong&gt;: No level counters or &lt;code&gt;max_depth&lt;/code&gt; variables.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Obvious Intent&lt;/strong&gt;: The code structure mirrors the problem's logic.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Advanced Patterns (Intervals, Sliding Window)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Interval Sorting
&lt;/h3&gt;

&lt;p&gt;When dealing with interval problems (e.g., Meeting Rooms, Merge Intervals, Interval Scheduling), the sorting criteria is critical.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Sort by Start Time (Default)
&lt;/h2&gt;

&lt;p&gt;Use this for &lt;strong&gt;Merge Intervals&lt;/strong&gt; or &lt;strong&gt;Meeting Rooms&lt;/strong&gt;. It helps you process intervals as they "arrive".&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Python sorts by the first element of the sub-lists by default
&lt;/span&gt;&lt;span class="n"&gt;intervals&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; 

&lt;span class="c1"&gt;# Explicitly:
&lt;/span&gt;&lt;span class="n"&gt;intervals&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Sort by End Time
&lt;/h2&gt;

&lt;p&gt;Use this for &lt;strong&gt;Interval Scheduling / Maximum Non-overlapping Intervals&lt;/strong&gt;. &lt;br&gt;
Picking the interval that finishes earliest (greedy) leaves the most space for others.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# MUST use key=lambda or itemgetter
&lt;/span&gt;&lt;span class="n"&gt;intervals&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="c1"&gt;# Or using itemgetter (slightly faster for large lists)
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;itemgetter&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;itemgetter&lt;/span&gt;
&lt;span class="n"&gt;intervals&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;itemgetter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pitfalls
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The Lambda Syntax&lt;/strong&gt;: &lt;code&gt;sorted(arr, lambda x: x[1])&lt;/code&gt; will error. You must use the &lt;code&gt;key=&lt;/code&gt; keyword argument: &lt;code&gt;sorted(arr, key=lambda x: x[1])&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;In-place vs. New List&lt;/strong&gt;: &lt;code&gt;intervals.sort()&lt;/code&gt; modifies the list in place and returns &lt;code&gt;None&lt;/code&gt;. &lt;code&gt;sorted(intervals)&lt;/code&gt; returns a new sorted list.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Comparing Start with End&lt;/strong&gt;: In overlap checks, always compare the &lt;strong&gt;current start&lt;/strong&gt; with the &lt;strong&gt;previous end&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Efficient Sliding Window (Fixed Size k)
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Elegant Refactor Pattern
&lt;/h3&gt;

&lt;p&gt;The most idiomatic way to write a fixed-size sliding window in Python is to iterate through the list once and use the loop index as your "right" boundary. This eliminates manual index incrementing and awkward "peek-ahead" logic.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;maxSum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# 1. Initial window sum
&lt;/span&gt;    &lt;span class="n"&gt;cur_sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="n"&gt;max_sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cur_sum&lt;/span&gt;

    &lt;span class="c1"&gt;# 2. Start from the first element AFTER the initial window
&lt;/span&gt;    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
        &lt;span class="c1"&gt;# Slide: Add the new element, subtract the one that fell off
&lt;/span&gt;        &lt;span class="n"&gt;cur_sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;cur_sum&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;max_sum&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;max_sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cur_sum&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;max_sum&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why this version feels better:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;No "Off-by-One" Anxiety:&lt;/strong&gt; By using &lt;code&gt;range(k, len(nums))&lt;/code&gt;, you eliminate the need to check &lt;code&gt;r + 1&lt;/code&gt;. The loop naturally stops when the data ends.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simplified Pointers:&lt;/strong&gt; You only manage one index (&lt;code&gt;i&lt;/code&gt;). The "left" side of your window is always just &lt;code&gt;i - k&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Readability:&lt;/strong&gt; It’s immediately clear that you are processing the array from the k-th element to the end.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Analogy
&lt;/h3&gt;

&lt;p&gt;That &lt;code&gt;while (r + 1) &amp;lt; len(nums)&lt;/code&gt; check feels a bit like &lt;strong&gt;trying to look over a fence while standing on your tiptoes&lt;/strong&gt;—it works, but it's not the most comfortable position. Letting the &lt;code&gt;for&lt;/code&gt; loop handle the pointer management for you is much more ergonomic.&lt;/p&gt;

&lt;h3&gt;
  
  
  Edge Cases
&lt;/h3&gt;

&lt;p&gt;In a real-world scenario or technical interview, always add a quick check:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This prevents the code from exploding if you receive an empty list or a window size of zero.&lt;/p&gt;




&lt;h3&gt;
  
  
  Range for Sliding Window
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# For window of size k: last valid start = len(nums) - k
# Use range(len(nums) - k + 1) to include all starting positions
&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;window&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Dummy Pointers for Traversal
&lt;/h3&gt;

&lt;p&gt;When traversing a linked list, use a &lt;strong&gt;dummy pointer&lt;/strong&gt; (often named &lt;code&gt;curr&lt;/code&gt; or &lt;code&gt;dummy&lt;/code&gt;) to iterate through the nodes instead of moving the &lt;code&gt;head&lt;/code&gt; pointer itself.&lt;/p&gt;

&lt;h3&gt;
  
  
  Importance
&lt;/h3&gt;

&lt;p&gt;Moving the &lt;code&gt;head&lt;/code&gt; pointer during a traversal causes you to lose the reference to the start of the list. By using a dummy pointer, you preserve the &lt;code&gt;head&lt;/code&gt; reference so you can return it or traverse the list again later.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;ans&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="c1"&gt;# Use a dummy pointer for traversal
&lt;/span&gt;    &lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;ans&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;
        &lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;

    &lt;span class="c1"&gt;# We still have the 'head' pointer at the start of the list
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;ans&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Fast and Slow Pointers
&lt;/h3&gt;

&lt;p&gt;The fast and slow pointer technique (also known as Tortoise and Hare) is a common pattern for linked list problems, such as finding the middle of a list or detecting a cycle.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementation
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# head is the head node of a linked list
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;slow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;
    &lt;span class="n"&gt;fast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# Do something here
&lt;/span&gt;        &lt;span class="n"&gt;slow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;slow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;
        &lt;span class="n"&gt;fast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why the &lt;code&gt;fast.next&lt;/code&gt; Check?
&lt;/h3&gt;

&lt;p&gt;The reason we need the &lt;code&gt;while&lt;/code&gt; condition to check for both &lt;code&gt;fast&lt;/code&gt; and &lt;code&gt;fast.next&lt;/code&gt; is to prevent an &lt;code&gt;AttributeError&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;fast&lt;/code&gt; is &lt;code&gt;None&lt;/code&gt;, the loop stops (handles even-length lists or empty lists).&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;fast&lt;/code&gt; is the final node, then &lt;code&gt;fast.next&lt;/code&gt; is &lt;code&gt;None&lt;/code&gt;. Trying to access &lt;code&gt;fast.next.next&lt;/code&gt; would result in an error (e.g., &lt;code&gt;AttributeError: 'NoneType' object has no attribute 'next'&lt;/code&gt;). Checking &lt;code&gt;fast.next&lt;/code&gt; ensures we only advance &lt;code&gt;fast&lt;/code&gt; when it is safe to skip two nodes ahead.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Common Pitfalls (Manual Counting)
&lt;/h3&gt;

&lt;p&gt;Initially, you might try to find the middle by counting nodes first. This is prone to "off-by-one" errors and messy &lt;code&gt;if&lt;/code&gt; statements.&lt;/p&gt;

&lt;h4&gt;
  
  
  The "Step Count" Trap
&lt;/h4&gt;

&lt;p&gt;If you count "steps" (using &lt;code&gt;while curr.next&lt;/code&gt;), you end up with &lt;code&gt;(length - 1)&lt;/code&gt; counts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;dummy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="c1"&gt;# Counting jumps, not nodes
&lt;/span&gt;    &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="n"&gt;dummy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dummy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;

&lt;span class="c1"&gt;# Wrong approach for even-length lists (returns first middle):
&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; 

&lt;span class="c1"&gt;# Correct approach without using an 'if':
&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  The "Node Count" Solution
&lt;/h4&gt;

&lt;p&gt;Counting total nodes (using &lt;code&gt;while curr&lt;/code&gt;) makes the math much cleaner:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="c1"&gt;# Counting nodes
&lt;/span&gt;    &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;

&lt;span class="c1"&gt;# Works for both odd and even lists (returns second middle):
&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using &lt;strong&gt;Fast and Slow Pointers&lt;/strong&gt; avoids this entire counting overhead and math logic.&lt;/p&gt;




&lt;h2&gt;
  
  
  Recursion &amp;amp; Caching
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Nested Functions &amp;amp; Scope (Closures)
&lt;/h3&gt;

&lt;p&gt;In Python, an internal (nested) function has access to the variables defined in its parent function's scope. This is called a &lt;strong&gt;closure&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Tip: Don't Re-Pass Outer Arguments
&lt;/h3&gt;

&lt;p&gt;Many people waste time passing variables like &lt;code&gt;target&lt;/code&gt;, &lt;code&gt;k&lt;/code&gt;, or &lt;code&gt;graph&lt;/code&gt; into their helper &lt;code&gt;dfs&lt;/code&gt; function. If those variables don't change, you don't need to pass them!&lt;/p&gt;

&lt;h3&gt;
  
  
  Redundant Passing
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;solution&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="c1"&gt;# &amp;lt;--- target is redundant
&lt;/span&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="c1"&gt;# logic
&lt;/span&gt;            &lt;span class="k"&gt;pass&lt;/span&gt;
        &lt;span class="nf"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nf"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Clean &amp;amp; Fast
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;solution&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="c1"&gt;# &amp;lt;--- target is inherited from outer scope
&lt;/span&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="c1"&gt;# logic
&lt;/span&gt;            &lt;span class="k"&gt;pass&lt;/span&gt;
        &lt;span class="nf"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nf"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Important: Mutation vs Access
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Accessing:&lt;/strong&gt; You can read any outer variable for free.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mutating:&lt;/strong&gt; If the outer variable is a list or dict, you can mutate it (e.g., &lt;code&gt;res.append(val)&lt;/code&gt;) WITHOUT &lt;code&gt;nonlocal&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Updating:&lt;/strong&gt; If you want to &lt;em&gt;reassign&lt;/em&gt; an outer variable (e.g., &lt;code&gt;count = count + 1&lt;/code&gt;), you have two choices:

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;The &lt;code&gt;nonlocal&lt;/code&gt; keyword:&lt;/strong&gt; Declare &lt;code&gt;nonlocal count&lt;/code&gt; inside the nested function.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;The &lt;code&gt;self&lt;/code&gt; pattern (Recommended):&lt;/strong&gt; Use a class member. This is often cleaner and avoids scope confusion.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The "Self" Pattern
&lt;/h3&gt;

&lt;p&gt;In LeetCode, since your code is inside a &lt;code&gt;Solution&lt;/code&gt; class, you can anchor state to the instance. This is often more "elegant" than &lt;code&gt;nonlocal&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;solve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;  &lt;span class="c1"&gt;# Anchor state to the instance
&lt;/span&gt;
        &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;# No nonlocal needed!
&lt;/span&gt;            &lt;span class="nf"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nf"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="nf"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Interview Why
&lt;/h3&gt;

&lt;p&gt;During an interview, typing &lt;code&gt;target&lt;/code&gt; or &lt;code&gt;res&lt;/code&gt; 5-6 extra times in your recursion adds up and increases the chance of a typo. Keep your internal signatures as small as possible—usually just &lt;code&gt;node&lt;/code&gt; and any state that &lt;em&gt;actually&lt;/em&gt; changes per call (like &lt;code&gt;curr_sum&lt;/code&gt; or &lt;code&gt;depth&lt;/code&gt;).&lt;/p&gt;




&lt;h3&gt;
  
  
  Built-in Memoization with &lt;a class="mentioned-user" href="https://dev.to/cache"&gt;@cache&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;When writing recursive functions (especially in dynamic programming problems like "Decode Ways" or "Fibonacci"), you risk hitting exponential time complexities because of redundant subtree calculations (e.g. &lt;code&gt;O(2^n)&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Python provides a built-in memory/cache notebook called &lt;code&gt;@cache&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Usage
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;functools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;cache&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nd"&gt;@cache&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

        &lt;span class="c1"&gt;# ... logic ...
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By adding &lt;code&gt;@cache&lt;/code&gt; directly above the recursive function, it will save the results of past function calls and reuse them if the same arguments are seen again. This drops the complexity from &lt;code&gt;O(2^n)&lt;/code&gt; to &lt;code&gt;O(n)&lt;/code&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Minimum Depth of Binary Tree Pitfall
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Single Child Trap
&lt;/h3&gt;

&lt;p&gt;When calculating the &lt;strong&gt;minimum&lt;/strong&gt; depth of a tree, a common mistake is to treat a missing child (&lt;code&gt;None&lt;/code&gt;) as a path of depth 0.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why the standard recursion fails
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#  INCORRECT LOGIC
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;minDepth&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;minDepth&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nf"&gt;minDepth&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In a tree where a node has only &lt;strong&gt;one&lt;/strong&gt; child (e.g., &lt;code&gt;1 -&amp;gt; 2&lt;/code&gt;), the logic above will:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;See &lt;code&gt;root.right&lt;/code&gt; is &lt;code&gt;None&lt;/code&gt; -&amp;gt; return &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Take &lt;code&gt;min(left_depth, 0) + 1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Conclude the min depth is &lt;code&gt;1&lt;/code&gt; (as if the root itself was a leaf).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Interview Logic Rules
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;leaf&lt;/strong&gt; is a node with &lt;strong&gt;no left AND no right&lt;/strong&gt; children. A missing child is an &lt;strong&gt;empty set of paths&lt;/strong&gt;, not a path of length 0.&lt;/p&gt;

&lt;h3&gt;
  
  
  Correct Conceptual Logic
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Both children exist&lt;/strong&gt;: Take &lt;code&gt;min(left, right) + 1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Only one child exists&lt;/strong&gt;: You &lt;strong&gt;must&lt;/strong&gt; follow that path. Return &lt;code&gt;1 + depth_of_existing_child&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No children (Leaf)&lt;/strong&gt;: Return &lt;code&gt;1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No root&lt;/strong&gt;: Return &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Depth Sentinel Logic
&lt;/h3&gt;

&lt;p&gt;"I must filter out sentinel values (0 for None) before using &lt;code&gt;min()&lt;/code&gt;. In &lt;code&gt;maxDepth&lt;/code&gt;, this doesn't matter because &lt;code&gt;0&lt;/code&gt; never wins against a positive depth, but in &lt;code&gt;minDepth&lt;/code&gt;, the sentinel 'cheats' the comparison."&lt;/p&gt;




&lt;h3&gt;
  
  
  13000 – The &lt;code&gt;@cache&lt;/code&gt; Decorator (Instant Memoization)
&lt;/h3&gt;

&lt;p&gt;In Python, you can convert a slow recursive function into a fast Dynamic Programming (DP) solution by adding a single line. This is the ultimate "cheat code" for top-down DP.&lt;/p&gt;

&lt;h3&gt;
  
  
  Automatic Memoization
&lt;/h3&gt;

&lt;p&gt;Instead of manually creating a &lt;code&gt;memo&lt;/code&gt; dictionary and checking &lt;code&gt;if state in memo&lt;/code&gt;, just use &lt;code&gt;@cache&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;functools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;cache&lt;/span&gt;

&lt;span class="nd"&gt;@cache&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;solve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# Standard recursive logic...
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Phrase -&amp;gt; Logic -&amp;gt; Code
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Phrase&lt;/strong&gt;: "Just remember what you did so you don't do it again."&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;The WHY&lt;/strong&gt;: Recursion without memoization is exponential (O(2^n)) because it re-solves the same subproblems. &lt;code&gt;@cache&lt;/code&gt; automatically stores function arguments as keys and results as values in a hidden dictionary, turning it into O(N * M).&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The Code&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;functools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;cache&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;climbStairs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nd"&gt;@cache&lt;/span&gt;
        &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Pitfalls &amp;amp; Requirements
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Hashable Arguments&lt;/strong&gt;: All arguments to the function (e.g., &lt;code&gt;i&lt;/code&gt;, &lt;code&gt;j&lt;/code&gt;, &lt;code&gt;state_tuple&lt;/code&gt;) &lt;strong&gt;must be hashable&lt;/strong&gt; (integers, strings, tuples). You cannot pass a &lt;code&gt;list&lt;/code&gt; or &lt;code&gt;set&lt;/code&gt; directly; convert them to a &lt;code&gt;tuple&lt;/code&gt; or &lt;code&gt;frozenset&lt;/code&gt; first.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Recursion Limit&lt;/strong&gt;: Large constraints might hit Python's default recursion limit (usually 1000). Use &lt;code&gt;sys.setrecursionlimit()&lt;/code&gt; if needed.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Python Version&lt;/strong&gt;: &lt;code&gt;@cache&lt;/code&gt; was added in Python 3.9. For older versions, use &lt;code&gt;@lru_cache(None)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  When to use it?
&lt;/h3&gt;

&lt;p&gt;Whenever you are writing &lt;strong&gt;Top-Down DP&lt;/strong&gt; or &lt;strong&gt;DFS with memoization&lt;/strong&gt;. It keeps the code clean and lets you focus on the transition logic rather than state management.&lt;/p&gt;




&lt;p&gt;Originally posted at: &lt;a href="https://looppass.mindmeld360.com/blog/the-living-giant-python-syntax-and-traps-leetcode-document/" rel="noopener noreferrer"&gt;https://looppass.mindmeld360.com/blog/the-living-giant-python-syntax-and-traps-leetcode-document/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>leetcode</category>
      <category>interview</category>
      <category>career</category>
    </item>
    <item>
      <title>Choosing the Right Shortest Path Algorithm</title>
      <dc:creator>Tomer Ben David</dc:creator>
      <pubDate>Sat, 11 Apr 2026 07:29:10 +0000</pubDate>
      <link>https://dev.to/tomerbendavid/choosing-the-right-shortest-path-algorithm-17f5</link>
      <guid>https://dev.to/tomerbendavid/choosing-the-right-shortest-path-algorithm-17f5</guid>
      <description>&lt;p&gt;Shortest path problems on LeetCode vary by constraint. Graphs can have weights, no weights, single source focuses, or all pairs requirements. Some have positive costs and others have negative costs. &lt;/p&gt;

&lt;p&gt;Each specific situation has a corresponding algorithm. Understanding the constraints of the graph dictates the strategy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Identifying the Graph
&lt;/h2&gt;

&lt;p&gt;Before writing code, verify the terrain.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Clear Graph
&lt;/h3&gt;

&lt;p&gt;The first question is whether every step costs the same. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Calculating degrees of separation in a social network or moving between cells in a maze means the costs are uniform.&lt;/li&gt;
&lt;li&gt;  Dealing with traffic where one road takes 5 minutes and another takes 50, flight prices, or effort means each step has a unique cost. These are weighted graphs.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Disguised Graph
&lt;/h3&gt;

&lt;p&gt;Sometimes the problem hides the graph.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;The Matrix:&lt;/strong&gt; A 2D grid where each cell is a node and valid moves are edges. If moving to an adjacent cell costs 1, it is a simple BFS.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;State transitions:&lt;/strong&gt; Consider Word Ladder. Each word is a node and a one character difference is the edge. Since every transform costs 1, this is a BFS problem.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Resource management:&lt;/strong&gt; Problems like Cheapest Flights Within K Stops are weighted graphs requiring you to track cost while adhering to state constraints.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Selection Logic
&lt;/h2&gt;

&lt;p&gt;Select the algorithm based on what the graph requires. &lt;/p&gt;

&lt;h3&gt;
  
  
  BFS
&lt;/h3&gt;

&lt;p&gt;If every step costs the same, use Breadth-First Search. The first time the search reaches a node is the shortest path. &lt;/p&gt;

&lt;h3&gt;
  
  
  Dijkstra
&lt;/h3&gt;

&lt;p&gt;When roads have different lengths but they are all positive, use Dijkstra. A discovery at one point in the search assumes no future path through a positive weight road can make it better. &lt;/p&gt;

&lt;h3&gt;
  
  
  Bellman-Ford
&lt;/h3&gt;

&lt;p&gt;If a path provides a negative cost, Dijkstra fails. Bellman-Ford handles negative weights and detects cycles where a path keeps getting cheaper forever. &lt;/p&gt;

&lt;h3&gt;
  
  
  Floyd-Warshall
&lt;/h3&gt;

&lt;p&gt;If the problem requires the shortest path from every node to every other node, use Floyd-Warshall. This checks every node as a possible layover to solve for all pairs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Escalation of Power
&lt;/h2&gt;

&lt;p&gt;As graph rules become more complex, the algorithms become heavier. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  BFS is fastest but cannot handle weights.&lt;/li&gt;
&lt;li&gt;  Dijkstra handles weights but requires a priority queue and fails on negative costs.&lt;/li&gt;
&lt;li&gt;  Bellman-Ford handles negatives and cycles but uses repeated loops.&lt;/li&gt;
&lt;li&gt;  Floyd-Warshall handles all pairs but uses triple nested loops.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Brute Force Hack
&lt;/h2&gt;

&lt;p&gt;You do not always need the most efficient algorithm to pass the interview. If you struggle to implement the minHeap logic for Dijkstra, use Bellman-Ford as a brute force alternative. &lt;/p&gt;

&lt;p&gt;You do not need a priority queue. Take the core idea of edge relaxation.&lt;/p&gt;

&lt;p&gt;Each pass through all edges discovers the shortest path using one additional edge. The first pass finds shortest paths with one edge, the second pass finds shortest paths with two edges, and so on. Since a shortest path in a graph of $V$ nodes can have at most $V-1$ edges, this ensures every node is covered. It is two nested loops and handles everything Dijkstra can.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# The brute force alternative
# n: number of nodes, edges: list of (u, v, weight)
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;shortest_path_hack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;edges&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;dist&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;inf&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
    &lt;span class="n"&gt;dist&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

    &lt;span class="c1"&gt;# Its just a nested loop and you could pass the in without Dijkstra.
&lt;/span&gt;    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;edges&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;dist&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;dist&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
                &lt;span class="n"&gt;dist&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dist&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;

    &lt;span class="c1"&gt;# No need to handle weighted and negative edges.
&lt;/span&gt;    &lt;span class="c1"&gt;# We skip this part of belman ford. Quick Win. Two Birds.
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;dist&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Originally published at: &lt;a href="https://looppass.mindmeld360.com/blog/choosing-shortest-path-algorithm/" rel="noopener noreferrer"&gt;https://looppass.mindmeld360.com/blog/choosing-shortest-path-algorithm/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>interview</category>
      <category>career</category>
      <category>algorithms</category>
      <category>faang</category>
    </item>
    <item>
      <title>System Design Interview - Designing from Invariants</title>
      <dc:creator>Tomer Ben David</dc:creator>
      <pubDate>Wed, 08 Apr 2026 06:50:13 +0000</pubDate>
      <link>https://dev.to/tomerbendavid/system-design-interview-designing-from-invariants-3ede</link>
      <guid>https://dev.to/tomerbendavid/system-design-interview-designing-from-invariants-3ede</guid>
      <description>&lt;h2&gt;
  
  
  Designing from Invariants
&lt;/h2&gt;

&lt;p&gt;Software architecture is frequently treated as an exercise in connecting infrastructure components. We often reach for Kafka, Redis, or microservice boundaries as if they are the building blocks of the business logic itself. But when tools come before logic, the resulting design prioritizes infrastructure choices over the problem they are meant to solve.&lt;/p&gt;

&lt;p&gt;A high reliability system does not start with a distributed queue or a complex workflow engine. It starts with the core constraints the invariants that make the system reliable. If you start by choosing your infrastructure before you have defined the logic that keeps your data correct, you are building complexity on an undefined foundation.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Distribution Trap
&lt;/h2&gt;

&lt;p&gt;Most designs become unmanageable because they assume every step of a business process must be distributed across new infrastructure from the beginning. &lt;/p&gt;

&lt;p&gt;In this style of design, the business logic is spread across a database, a queue, and a workflow engine. To answer a simple question like &lt;em&gt;"What is the state of this payment?"&lt;/em&gt;, you have to reconstruct the story from multiple logs. This introduces the Dual Write problem where a database update succeeds but a message publish fails before the system has even achieved its basic purpose.&lt;/p&gt;

&lt;h2&gt;
  
  
  Coherence as the Minimal Solution
&lt;/h2&gt;

&lt;p&gt;The strongest designs identify the &lt;strong&gt;Invariants&lt;/strong&gt; first. An invariant is a statement that must always be true for the business to be valid. For example:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"A cleared risk decision must never exist without an authoritative payment record."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If the business rules require two things to change together to be valid, the simplest and most robust solution is to keep them in the same transaction. &lt;/p&gt;

&lt;p&gt;This logical anchor is the &lt;strong&gt;Transactional Center&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;The core state machine for an important process should have one queryable home, usually a relational database like Postgres. By starting here, you eliminate entire classes of distributed system bugs. You can scale the system outward later, but the authority remains in one place.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scaling without Scattering
&lt;/h2&gt;

&lt;p&gt;Scaling should be a reaction to a requirement, not a default architecture. The &lt;strong&gt;Four Plane Model&lt;/strong&gt; provides a way to distribute workloads without losing the source of truth.&lt;/p&gt;

&lt;h3&gt;
  
  
  Plane 1 Transactional Truth
&lt;/h3&gt;

&lt;p&gt;This is the core. It owns the current state, the audit trail, and the records used to reliably notify the rest of the system. &lt;/p&gt;

&lt;h3&gt;
  
  
  Plane 2 Action Systems
&lt;/h3&gt;

&lt;p&gt;These are Kafka workers and background jobs. They &lt;strong&gt;react&lt;/strong&gt; to the truth committed in Plane 1. Asynchronous tasks like notifications or external fraud checks happen here without slowing down the core transaction.&lt;/p&gt;

&lt;h3&gt;
  
  
  Plane 3 Real Time Reads
&lt;/h3&gt;

&lt;p&gt;When you need fast dashboards, move those reads to a specialized replica like ClickHouse. This keeps analytical traffic from overwhelming the transactional core.&lt;/p&gt;

&lt;h3&gt;
  
  
  Plane 4 Historical Analytics
&lt;/h3&gt;

&lt;p&gt;This is for deep history and data science (BigQuery or Snowflake). It stays completely separate from the operational system.&lt;/p&gt;

&lt;h2&gt;
  
  
  Choosing Your Path
&lt;/h2&gt;

&lt;p&gt;The decision to distribute should always follow the logic of the problem.&lt;/p&gt;

&lt;h3&gt;
  
  
  Start with a Transactional Center when
&lt;/h3&gt;

&lt;p&gt;Consistency is part of the business value. If a payment must be atomic with an order update, keep them together. This is the simplest possible solution and the most resilient to failure.&lt;/p&gt;

&lt;h3&gt;
  
  
  Extend to Distributed Choreography when
&lt;/h3&gt;

&lt;p&gt;Domains are truly independent or you have reached a scale where a single database cannot handle the write volume. Use patterns like Sagas only when the local boundary can no longer support the technical requirements of the system.&lt;/p&gt;

&lt;p&gt;A resilient system starts by identifying the center. Ask one question: &lt;strong&gt;Where is the authority?&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;Originally published at: &lt;a href="https://looppass.mindmeld360.com/blog/system-design-transactional-center/" rel="noopener noreferrer"&gt;https://looppass.mindmeld360.com/blog/system-design-transactional-center/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>career</category>
      <category>architecture</category>
      <category>distributedsystems</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>Memory Types in LangChain</title>
      <dc:creator>Tomer Ben David</dc:creator>
      <pubDate>Sun, 15 Mar 2026 09:18:43 +0000</pubDate>
      <link>https://dev.to/tomerbendavid/memory-types-in-langchain-4l0n</link>
      <guid>https://dev.to/tomerbendavid/memory-types-in-langchain-4l0n</guid>
      <description>&lt;h3&gt;
  
  
  Ever felt like your LLM needs a memory?
&lt;/h3&gt;

&lt;p&gt;LangChain felt the same thing. From full chat transcripts to summaries, entities, and vector backed recall, it gives you several ways to make a stateless model feel like it actually remembers what matters.&lt;/p&gt;

&lt;p&gt;Large Language Models are inherently stateless. Every request you send arrives as a blank slate with no recollection of what was discussed five minutes ago. To create a coherent conversation, the system must manually feed previous messages back into the model. &lt;/p&gt;

&lt;p&gt;LangChain provides several distinct patterns for managing this history. Choosing the right one is a balance between providing perfect context and managing the cost of every token.&lt;/p&gt;

&lt;h3&gt;
  
  
  LangChain Memory Types
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use the &lt;strong&gt;Transcript Pattern&lt;/strong&gt; for quick, high precision support tasks.&lt;/li&gt;
&lt;li&gt;Use the &lt;strong&gt;Window Pattern&lt;/strong&gt; for predictable, task oriented interactions.&lt;/li&gt;
&lt;li&gt;Use the &lt;strong&gt;Summary Pattern&lt;/strong&gt; for long, creative, or collaborative sessions.&lt;/li&gt;
&lt;li&gt;Use the &lt;strong&gt;Entity Pattern&lt;/strong&gt; for personal assistants that track user preferences.&lt;/li&gt;
&lt;li&gt;Use the &lt;strong&gt;Vector Retrieval&lt;/strong&gt; Pattern for knowledge intensive systems with vast histories.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Transcript Pattern
&lt;/h3&gt;

&lt;p&gt;The simplest way to maintain a conversation is through a direct buffer. This stores every word exactly as it was spoken in a sequential list.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every message from the user and every response from the AI is saved verbatim.&lt;/li&gt;
&lt;li&gt;The entire history is appended to the prompt for the next turn.&lt;/li&gt;
&lt;li&gt;It provides the model with the most accurate and raw context possible.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain.memory&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ConversationBufferMemory&lt;/span&gt;

&lt;span class="n"&gt;memory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ConversationBufferMemory&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;memory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save_context&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;input&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;What is the capital of France?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;output&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;The capital of France is Paris.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="n"&gt;memory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load_memory_variables&lt;/span&gt;&lt;span class="p"&gt;({})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An example of this is a customer support bot helping a user reset a password. The bot needs to remember the specific email address and the error code mentioned two sentences ago to provide a precise solution. While excellent for short interactions, this does not scale for long sessions where the prompt becomes massive.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Window Pattern
&lt;/h3&gt;

&lt;p&gt;To solve the scaling issue of a raw buffer, we can use a sliding window. This strategy only keeps the most recent portion of the conversation.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The system only remembers the last few interactions, defined by a fixed count.&lt;/li&gt;
&lt;li&gt;Older segments are discarded as new ones arrive.&lt;/li&gt;
&lt;li&gt;This keeps the prompt size and API costs predictable.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain.memory&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ConversationBufferWindowMemory&lt;/span&gt;

&lt;span class="n"&gt;memory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ConversationBufferWindowMemory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;memory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save_context&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;input&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;I live in London&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;output&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;London is a great city.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="n"&gt;memory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save_context&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;input&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;What is the weather like?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;output&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;It is currently rainy in London.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A weather assistant is a perfect candidate for this pattern. If you ask for the forecast in London and then ask "What about tomorrow?", the bot only needs the most recent context to understand that you are still talking about London. It does not need to remember that you asked about the news ten minutes ago.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Summary Pattern
&lt;/h3&gt;

&lt;p&gt;For very long term dialogues, a summarization strategy is more effective. Instead of saving every word, the system maintains a running overview of the discussion.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;After each interaction, the system updates a concise summary of the key points.&lt;/li&gt;
&lt;li&gt;Only this summary is sent to the primary model as context.&lt;/li&gt;
&lt;li&gt;It handles massive transcripts while keeping the context size relatively flat.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain.memory&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ConversationSummaryMemory&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain_openai&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;OpenAI&lt;/span&gt;

&lt;span class="n"&gt;llm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OpenAI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;temperature&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;memory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ConversationSummaryMemory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;llm&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;llm&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;memory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save_context&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;input&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Explain the plot of Inception&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;output&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Inception is about dreams within dreams...&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Consider a creative writing assistant helping you plot a novel. Over several hours, you might discuss dozens of characters and plot points. Instead of feeding the whole transcript, the system carries a summary that tracks the main objective and the current state of the story.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Entity Pattern
&lt;/h3&gt;

&lt;p&gt;Some applications require remember specific facts about people or technical concepts without carrying the entire dialogue.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The system extracts key participants or topics mentioned in the chat.&lt;/li&gt;
&lt;li&gt;It builds a structured knowledge base about these specific items.&lt;/li&gt;
&lt;li&gt;Relevant facts are pulled from storage when the topic resurfaces.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain.memory&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ConversationEntityMemory&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain_openai&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;OpenAI&lt;/span&gt;

&lt;span class="n"&gt;llm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OpenAI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;temperature&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;memory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ConversationEntityMemory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;llm&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;llm&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;memory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save_context&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;input&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;My name is Tomer and I use Kotlin&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;output&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Nice to meet you Tomer.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An example is a personalized coding coach. If you mention that you prefer a specific library like React or a particular cloud provider, the system stores that fact. When you later ask for a code sample, it automatically applies those preferences without needing to reread the original transcript.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Vector Retrieval Pattern
&lt;/h3&gt;

&lt;p&gt;The most advanced method involves treating the conversation like a database. This allows the model to recall information from any point in the history based on semantic relevance.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Past message snippets are stored in a vector database.&lt;/li&gt;
&lt;li&gt;The system performs a search based on the current user query.&lt;/li&gt;
&lt;li&gt;It retrieves only the most relevant historical segments.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain.memory&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;VectorStoreRetrieverMemory&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;faiss&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain_community.docstore&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;InMemoryDocstore&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain_community.vectorstores&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;FAISS&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain_openai&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;OpenAIEmbeddings&lt;/span&gt;

&lt;span class="n"&gt;vectorstore&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FAISS&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;OpenAIEmbeddings&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;embed_query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;faiss&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;IndexFlatL2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1536&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nc"&gt;InMemoryDocstore&lt;/span&gt;&lt;span class="p"&gt;({}),&lt;/span&gt; &lt;span class="p"&gt;{})&lt;/span&gt;
&lt;span class="n"&gt;retriever&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;vectorstore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;as_retriever&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;search_kwargs&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;memory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;VectorStoreRetrieverMemory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;retriever&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;retriever&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the ideal choice for an AI researcher. If you are discussing a series of academic papers over several weeks, the model can pull a specific detail from a conversation you had ten days ago because it is semantically related to your current question.&lt;/p&gt;




&lt;p&gt;Originally published at: &lt;a href="https://looppass.mindmeld360.com/blog/langchain-memory-types/" rel="noopener noreferrer"&gt;https://looppass.mindmeld360.com/blog/langchain-memory-types/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>langchain</category>
      <category>coding</category>
      <category>llm</category>
    </item>
    <item>
      <title>The Battle Between RAG and Long Context</title>
      <dc:creator>Tomer Ben David</dc:creator>
      <pubDate>Fri, 13 Mar 2026 06:27:21 +0000</pubDate>
      <link>https://dev.to/tomerbendavid/the-battle-between-rag-and-long-context-4ilc</link>
      <guid>https://dev.to/tomerbendavid/the-battle-between-rag-and-long-context-4ilc</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;Large Language Models arrive with a fundamental limitation known as the knowledge cutoff. They are experts on the world as it existed during their training phase but they are completely blind to your private data or events that happened this morning. Whether it is an internal wiki or a complex codebase, the model cannot see what it was not trained on. To make these systems useful for building products, we have to solve the problem of context injection.&lt;/p&gt;

&lt;p&gt;The industry is currently split between two competing philosophies for solving this. One is a complex engineering pipeline while the other is a brute force architectural shift.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Engineering Complexity of Retrieval Augmented Generation
&lt;/h3&gt;

&lt;p&gt;Retrieval Augmented Generation is the established path for providing context. It works by turning your entire knowledge base into a searchable index. You break your documents into small pieces and store them in a vector database as numerical maps. When a user submits a query, the system performs a semantic search to find the most relevant snippets and hands them to the model for processing.&lt;/p&gt;

&lt;p&gt;This remains the essential strategy for massive datasets. If you have ten million technical specifications, you cannot possibly cram them all into a single prompt. This approach acts as a smart filter that protects the model from information overload. It is also more cost efficient for high volume systems because you only pay to process a few hundred words of context instead of millions of tokens every time. &lt;/p&gt;

&lt;p&gt;However, this method introduces a retrieval lottery. If your search logic fails to find the exact piece of information required, the model will never see it. You are essentially gambling that your search engine is smart enough to find the needle in a global haystack.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Simplicity of Long Context Brute Force
&lt;/h3&gt;

&lt;p&gt;A newer alternative is to use models with massive context windows. Instead of building a complex database and retrieval pipeline, you simply paste your entire dataset directly into the prompt. This has been called the no stack stack because it removes the need for infrastructure like vector databases and embedding models entirely.&lt;/p&gt;

&lt;p&gt;The primary advantage here is global reasoning. When you give the model every word of the source material, you eliminate the risk of the retrieval lottery. This is superior for tasks that require seeing the whole picture. For example, if you are analyzing a series of incident reports from a distributed system to find a recurring pattern, you want the model to see every log entry simultaneously. In a traditional retrieval system, the search might pull out isolated errors but miss the subtle connection between a load balancer change on Monday and a latency spike on Thursday. By providing the entire history at once, you allow the model to detect deep architectural threads.&lt;/p&gt;

&lt;p&gt;The downside is the token tax. You pay the price for every word in your knowledge base on every single turn. These systems can also suffer from attention dilution. When you overwhelm a model with too much information, it may start to ignore or misinterpret details that are buried in the middle of a massive block of text.&lt;/p&gt;

&lt;h3&gt;
  
  
  Navigating the Infinite Data Problem
&lt;/h3&gt;

&lt;p&gt;For many enterprise environments, the data lake is effectively infinite. A million tokens might sound like a lot, but it is a drop in the ocean compared to the size of a global corporate knowledge base. In these scenarios, retrieval is not just an option but a structural necessity. You cannot brute force a petabyte of data into a prompt regardless of how large the context window becomes.&lt;/p&gt;

&lt;p&gt;The choice comes down to the boundaries of your problem. You should use the long context approach for bounded datasets that require deep and interconnected reasoning across every page. You should stick with the engineering approach when you need to navigate vast libraries of information where efficiency and noise reduction are the highest priorities.&lt;/p&gt;




&lt;p&gt;Originally posted at: &lt;a href="https://looppass.mindmeld360.com/blog/rag-vs-long-context-strategy/" rel="noopener noreferrer"&gt;https://looppass.mindmeld360.com/blog/rag-vs-long-context-strategy/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>architecture</category>
      <category>rag</category>
      <category>llm</category>
    </item>
    <item>
      <title>Comparing LangChain, CrewAI, and ADK</title>
      <dc:creator>Tomer Ben David</dc:creator>
      <pubDate>Thu, 12 Mar 2026 08:14:40 +0000</pubDate>
      <link>https://dev.to/tomerbendavid/comparing-langchain-crewai-and-adk-491j</link>
      <guid>https://dev.to/tomerbendavid/comparing-langchain-crewai-and-adk-491j</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;In the current gold rush of Agentic AI, developers are often caught in Framework Fatigue. Every week, a new library claims to be the standard for building autonomous agents. &lt;/p&gt;

&lt;p&gt;The question isn't only about which tool is most popular or which architecture solves a function. Different projects have different requirements, so the real challenge is finding the architecture that best matches your specific needs and your unique friction. &lt;/p&gt;

&lt;p&gt;You also have to balance this with your instincts about which framework might catch on as the de facto industry standard. If one of them wins, you want to be on the right side of that curve without sacrificing your specific goals today.&lt;/p&gt;

&lt;h3&gt;
  
  
  AI Coding and Building Your Own Orchestration
&lt;/h3&gt;

&lt;p&gt;Before we talk about ready made frameworks like LangChain or ADK, we have to acknowledge how the landscape has changed. In the era of AI coding, you don't necessarily need a massive library to get ahead. You can build your own bespoke orchestration layer that fits your project exactly.&lt;/p&gt;

&lt;p&gt;When you take the custom orchestration route, you are essentially solving three core technical challenges on your own terms.&lt;/p&gt;

&lt;p&gt;First is the &lt;strong&gt;Parsing Tax&lt;/strong&gt;. You need a way to ensure the AI returns structured data like JSON instead of just a paragraph of text. Today this is often solved with simple system prompts or native model features.&lt;/p&gt;

&lt;p&gt;Second is &lt;strong&gt;State Management&lt;/strong&gt;. You have to decide how the system remembers previous steps without overflowing the context window. &lt;/p&gt;

&lt;p&gt;Third is &lt;strong&gt;Loop Control&lt;/strong&gt;. You need a safety mechanism so an autonomous agent doesn't get stuck in a thought loop and burn through API credits.&lt;/p&gt;

&lt;p&gt;The choice today isn't about whether you can build an agent without a library. You definitely can, and for many uncommon projects, building your own thin orchestration is the best way to avoid unnecessary bloat.&lt;/p&gt;

&lt;h3&gt;
  
  
  LangChain and the Modular Lego Set
&lt;/h3&gt;

&lt;p&gt;LangChain was the first to standardize the chaos. It treated AI workflows like a pipeline or a "Chain."&lt;/p&gt;

&lt;p&gt;The philosophy here is modularity. Everything is a &lt;strong&gt;component&lt;/strong&gt;, including prompts, models, output parsers, and tools. &lt;/p&gt;

&lt;p&gt;If you need to take a PDF, turn it into vectors, and ask a question, LangChain has a plug for every single part of that process.&lt;/p&gt;

&lt;p&gt;The critique for many is that it became a &lt;strong&gt;"Thick Platform."&lt;/strong&gt; The abstractions can sometimes be harder to debug than the raw code itself. It is a massive toolkit that occasionally forces you to learn the LangChain way instead of the standard software engineering way.&lt;/p&gt;

&lt;h3&gt;
  
  
  CrewAI and the Collaborative Storyteller
&lt;/h3&gt;

&lt;p&gt;As we moved from single chains to Multi Agent Systems, CrewAI arrived with a different mental model of Role Playing.&lt;/p&gt;

&lt;p&gt;The philosophy is simple. Don't just give an agent a tool but give it a job. You define a Researcher, a Writer, and a Manager.&lt;/p&gt;

&lt;p&gt;It is important to understand that CrewAI is actually built on top of LangChain. It uses the foundational pieces of LangChain to handle the heavy lifting of LLM communication and tool execution while adding the collaborative crew logic on top. It is best for content creation or complex research because it excels at delegating tasks between agents. In these scenarios, it feels less like coding a system and more like managing a crew.&lt;/p&gt;

&lt;p&gt;The critique is that because it sits on top of LangChain, it inherits all of that platform's complexity. It is excellent for story driven workflows but can feel like it has too much magic under the hood for high precision systems engineering.&lt;/p&gt;

&lt;h3&gt;
  
  
  ADK or Google’s Agent Builder Kit
&lt;/h3&gt;

&lt;p&gt;ADK is the production first response. Unlike CrewAI, ADK is a standalone stack that doesn't rely on LangChain. It is a clean slate alternative.&lt;/p&gt;

&lt;p&gt;The philosophy treats agents as independent tools that you can plug into any system. It prioritizes writing real code and testing everything on your own machine before going live. While other frameworks can do hierarchy, ADK makes it a core structural primitive by treating entire agents as modular tools that a primary agent can call. It feels much like a system of nested microservices.&lt;/p&gt;

&lt;p&gt;This is the best case for enterprise environments where observability and Agent to Agent communication are critical. It’s optimized for Gemini but stays model agnostic via LiteLLM.&lt;/p&gt;

&lt;p&gt;The real strength here is that it treats an agent as a unit of deployment. This means the agent isn't just a variable in your code but a standalone service you can ship independently. For example, if you have a Pricing Agent. In a traditional library, that agent is just a function call inside your main application. If you want to update it, you have to redeploy your entire app. With ADK, that Pricing Agent is a standalone service with its own endpoint. You can update it, test it, or scale it without ever touching your main product code. It covers the entire engineering lifecycle, which includes professional evaluation, automated deployment, and production monitoring.&lt;/p&gt;

&lt;h3&gt;
  
  
  One Weather Task and Three Different Mental Models
&lt;/h3&gt;

&lt;p&gt;To see the difference clearly, lets say we want an agent to check the weather and suggest an outfit. Each framework approaches this differently.&lt;/p&gt;

&lt;p&gt;With LangChain, you build a chain of thought. You create a weather tool, give it to an agent executor, and the system runs a loop until it reaches the final answer. You are essentially building a custom logic path.&lt;/p&gt;

&lt;p&gt;With CrewAI, you would hire a Weather Expert and a Fashion Stylist. You define their roles and backstories, then assign them a task to collaborate. The Researcher finds the data and the Stylist uses it. You are managing a team meeting.&lt;/p&gt;

&lt;p&gt;With ADK, you define a Weather Service as a tool. You create a Weather Agent as a modular unit. Because it is hierarchical, you might have a primary Assistant Agent that simply delegates the request to that specialized unit. In this model, these agents can behave like actual web services that you communicate with via REST APIs. You have total flexibility here. You can choose to run every agent on a single monolithic server if your project is small or you can choose to have specialized agents living on different machines entirely. This allows your system to grow from a simple monolith into a network of independent services that you can update and scale one by one without touching the rest of the codebase. You are architecting for future growth instead of being locked into a single monolithic script.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Framework Paradox: Avoiding the J2EE Trap
&lt;/h3&gt;

&lt;p&gt;In software history, we often see a pendulum swing between "lightweight libraries" and "heavy platforms." For those who remember the early days of Enterprise Java, the term &lt;strong&gt;J2EE&lt;/strong&gt; often brings back memories of "Thick Platforms" that were so heavy you spent more time configuring the framework than writing the business logic.&lt;/p&gt;

&lt;p&gt;The risk with AI frameworks today is falling into that same trap. You start with a tool meant to simplify a task, but as the framework grows to cover every possible edge case, it introduces so much architectural weight that it becomes a burden. &lt;/p&gt;

&lt;p&gt;There is a delicate balance to strike. You want enough abstraction to be productive, but not so much that you lose sight of the underlying LLM calls. If you find yourself spending days trying to figure out how to "pass a variable the framework way" instead of just writing a function, you might be carrying too much weight.&lt;/p&gt;

&lt;h3&gt;
  
  
  Choosing the Right Path for Your Agent Architecture
&lt;/h3&gt;

&lt;p&gt;If you’ve followed my work at MindMeld360, you know I’m wary of Thick Platforms, but the truth is there is no single winner in this space yet. The industry is currently obsessed with finding the perfect library, but the real engineering task is matching the right abstraction level to each specific service you build.&lt;/p&gt;

&lt;p&gt;LangChain is a library of parts. CrewAI is a framework for behavior. ADK is a kit for modular systems.&lt;/p&gt;

&lt;p&gt;My advice is to start by playing with a custom and thin orchestration layer. You have to understand the problem space first and truly feel the pain that these frameworks are trying to solve. Once you gain your own intuition through a bespoke solution, you can incorporate existing libraries to handle the heavy lifting.&lt;/p&gt;

&lt;p&gt;Do not try to build your own massive agent library from scratch for production since these tools are already heavily used and battle tested. Instead, use a stage based approach to grow your experience.&lt;/p&gt;

&lt;p&gt;Start custom to feel the domain. Then build your next service with LangChain to see the ecosystem and the drawbacks for yourself.&lt;/p&gt;

&lt;p&gt;From there, you can choose the right tool for each job. Use LangChain when you want a common and widely supported library. Use CrewAI when you need a higher level of agent collaboration. Use ADK when you want to distribute your agents as independent services across a network.&lt;/p&gt;

&lt;h3&gt;
  
  
  Closing Note
&lt;/h3&gt;

&lt;p&gt;By the time this post has been published, we probably already have 5 more libraries to explore! The pace of AI is relentless, but that’s not a bad thing it just means more tools for us to master. More blog posts to come on those, so stay tuned! :)&lt;/p&gt;




&lt;p&gt;Originally published at: &lt;a href="https://looppass.mindmeld360.com/blog/ai-frameworks-langchain-crewai-adk/" rel="noopener noreferrer"&gt;https://looppass.mindmeld360.com/blog/ai-frameworks-langchain-crewai-adk/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>agents</category>
      <category>engineering</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Load Balancing &amp; WebSockets (L4 vs L7)</title>
      <dc:creator>Tomer Ben David</dc:creator>
      <pubDate>Mon, 09 Mar 2026 13:01:24 +0000</pubDate>
      <link>https://dev.to/tomerbendavid/load-balancing-websockets-l4-vs-l7-5b94</link>
      <guid>https://dev.to/tomerbendavid/load-balancing-websockets-l4-vs-l7-5b94</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;When you build a standard web app, load balancing is usually straightforward because every request is independent. You just spread the traffic around. But once you introduce WebSockets, everything changes. You are no longer dealing with quick requests. You are managing a persistent pipe that might stay open for hours.&lt;/p&gt;

&lt;p&gt;The first thing to understand is that WebSockets can work on either Layer 4 or Layer 7. There is no hard rule requiring one over the other. Every load balancer can pass a pocket of bits through. The difference is entirely in how the device treats the connection once it is established.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Layer 4 handles the traffic
&lt;/h2&gt;

&lt;p&gt;Since WebSockets are built on top of TCP, a Layer 4 load balancer can handle them perfectly. Think of this balancer as a high speed postman who only reads the house number on the envelope. He doesn't know he is routing WebSockets or HTTP. He just sees a raw TCP connection request on a specific port and blindly forwards that stream to a backend server.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;This approach works at the TCP level so it is incredibly efficient.&lt;/li&gt;
&lt;li&gt;The initial HTTP Upgrade request passes right through the load balancer. The backend server itself handles the handshake and the SSL termination.&lt;/li&gt;
&lt;li&gt;It can handle millions of simultaneous connections without breaking a sweat because it doesn't have to decrypt SSL or parse headers.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The main downside mentioned in architectural circles is the NAT trap. Because a Layer 4 balancer only sees IP addresses and ports, it often relies on the source IP to kept the connection sticky. If you have thousands of users in a single office building all sharing one public IP address, the balancer might accidentally send every single one of them to the same backend server. That server will quickly get overwhelmed while the rest of your fleet sits idle.&lt;/p&gt;

&lt;h2&gt;
  
  
  The intelligence of Layer 7
&lt;/h2&gt;

&lt;p&gt;A Layer 7 load balancer operates at the Application layer and actually understands the HTTP protocol. It is more like a sophisticated concierge who opens the mail to understand exactly who it is for and what they need. This balancer intercepts the traffic, decrypts the SSL, and reads the HTTP headers.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It explicitly sees the Upgrade and Connection headers that define a WebSocket.&lt;/li&gt;
&lt;li&gt;Because it reads the headers and cookies, it can route users based on session IDs rather than IP addresses. This completely avoids the NAT trap because every user has a unique cookie even if they share an IP.&lt;/li&gt;
&lt;li&gt;You can use path based routing to send specific types of traffic to different server groups. You could send chat traffic to one group and live feeds to another.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The performance trade off here is significant. The balancer has to maintain the state of the persistent WebSocket connection while continuously proxying the decrypted frames back and forth. This requires significantly more RAM and CPU than a simpler Layer 4 setup.&lt;/p&gt;

&lt;h2&gt;
  
  
  The hybrid approach for global scale
&lt;/h2&gt;

&lt;p&gt;Many massive global applications like Discord and Slack do not choose just one layer. They use a hybrid approach that provides the best of both worlds. They place highly resilient hardware Layer 4 balancers at the network edge to absorb massive traffic spikes and defend against DDoS attacks.&lt;/p&gt;

&lt;p&gt;These edge balancers then distribute the traffic to a internal fleet of software based Layer 7 balancers like NGINX or HAProxy. This second fleet handles the smart routing and the persistence needed for the WebSocket lifecycle. This layered strategy provides the raw horsepower to handle the initial connection and the intelligence to manage the application state once it is established.&lt;/p&gt;




&lt;p&gt;Originally published at: &lt;a href="https://looppass.mindmeld360.com/blog/load-balancing-websockets-l4-l7/" rel="noopener noreferrer"&gt;https://looppass.mindmeld360.com/blog/load-balancing-websockets-l4-l7/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>network</category>
      <category>architecture</category>
      <category>cloud</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>How to Actually use Python's heapq for Kth Largest Problems</title>
      <dc:creator>Tomer Ben David</dc:creator>
      <pubDate>Sun, 08 Mar 2026 09:41:45 +0000</pubDate>
      <link>https://dev.to/tomerbendavid/how-to-actually-use-pythons-heapq-for-kth-largest-problems-5138</link>
      <guid>https://dev.to/tomerbendavid/how-to-actually-use-pythons-heapq-for-kth-largest-problems-5138</guid>
      <description>&lt;p&gt;If you're using Python for coding interviews, &lt;code&gt;heapq&lt;/code&gt; is your best choice for priority queues. But it has a massive quirk that trips up almost everyone. &lt;strong&gt;It only supports min heaps.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you try to use &lt;code&gt;heapq.heapify_max()&lt;/code&gt;, your code will crash on most platforms (it's not fully public until Python 3.14).&lt;/p&gt;

&lt;p&gt;So, how do you find the Kth &lt;em&gt;largest&lt;/em&gt; element if you only have a &lt;em&gt;min&lt;/em&gt; heap? &lt;/p&gt;

&lt;p&gt;There is a brute force way, and there is the way interviewers actually want to see.&lt;/p&gt;

&lt;h2&gt;
  
  
  Brute force with negation
&lt;/h2&gt;

&lt;p&gt;Since &lt;code&gt;heapq&lt;/code&gt; always puts the smallest element at index 0, you can fake a max heap by making all your numbers negative. The largest positive number becomes the smallest negative number.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;heapq&lt;/span&gt;

&lt;span class="n"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;max_heap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;heapq&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;heapify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_heap&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# The root is now -6
&lt;/span&gt;&lt;span class="n"&gt;largest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;max_heap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This works fine for small arrays. But if an interviewer asks you to get the top 100 values from a stream of a billion numbers, storing every single number in memory is extremely inefficient. You need a better strategy.&lt;/p&gt;

&lt;h2&gt;
  
  
  The efficient Min heap strategy
&lt;/h2&gt;

&lt;p&gt;Instead of putting all the numbers into a max heap, put exactly &lt;code&gt;K&lt;/code&gt; numbers into a min heap.&lt;/p&gt;

&lt;p&gt;Think of it like keeping a running "Top 10" list. The root of a min heap (&lt;code&gt;heap[0]&lt;/code&gt;) is always the smallest element. If your heap is exactly size &lt;code&gt;K&lt;/code&gt;, the root is the smallest of your top &lt;code&gt;K&lt;/code&gt; numbers. &lt;/p&gt;

&lt;p&gt;As you stream through the rest of the data, if you see a new number that is bigger than your root, it belongs in the Top K. You kick the root out, and put the new number in.&lt;/p&gt;

&lt;p&gt;First, you start by creating a heap with only the first &lt;code&gt;K&lt;/code&gt; elements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;heapq&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;find_kth_largest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# Start our list with the first K elements
&lt;/span&gt;    &lt;span class="n"&gt;heap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;heapq&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;heapify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then you iterate through the remaining numbers. If a new number is larger than the root of our heap, it means the root is no longer in the Top K. You replace it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;    &lt;span class="c1"&gt;# Go through the rest of the numbers
&lt;/span&gt;    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
            &lt;span class="n"&gt;heapq&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;heapreplace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, the root of your heap will be the Kth largest element overall.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why Interviewers Care
&lt;/h2&gt;

&lt;p&gt;This exact pattern solves the massive streaming data problem perfectly. &lt;/p&gt;

&lt;p&gt;Because you only ever store &lt;code&gt;K&lt;/code&gt; elements at a time, your Space Complexity is &lt;code&gt;O(K)&lt;/code&gt;. It takes virtually zero memory. &lt;/p&gt;

&lt;p&gt;Your Time Complexity is &lt;code&gt;O(N log K)&lt;/code&gt;. You look at every number once (&lt;code&gt;N&lt;/code&gt;), and occasionally do a heap replacement operation that takes logarithmic time based on the small size of &lt;code&gt;K&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So next time you are asked for the &lt;code&gt;K&lt;/code&gt; largest items, do not reach for a max heap. Use a min heap, cap it at size &lt;code&gt;K&lt;/code&gt;, and only let the big numbers in.&lt;/p&gt;

</description>
      <category>python</category>
      <category>interview</category>
      <category>career</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Integrating Local GenAI into Desktop Applications: Lessons from RexIDE</title>
      <dc:creator>Tomer Ben David</dc:creator>
      <pubDate>Wed, 04 Feb 2026 00:00:00 +0000</pubDate>
      <link>https://dev.to/tomerbendavid/integrating-local-genai-into-desktop-applications-lessons-from-rexide-1l14</link>
      <guid>https://dev.to/tomerbendavid/integrating-local-genai-into-desktop-applications-lessons-from-rexide-1l14</guid>
      <description>&lt;p&gt;&lt;strong&gt;How we navigated the engineering challenges of embedding local AI models and agentic CLIs directly into a native desktop environment.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;RexIDE started as a personal frustration.&lt;/p&gt;

&lt;p&gt;Modern IDEs are powerful, but they weren’t designed for a world where AI agents are &lt;em&gt;active participants&lt;/em&gt; in your workflow. They assume short lived commands, stateless tools, and human only context switching. That model breaks down the moment you introduce long running AI agents, real terminals, and multi project execution.&lt;/p&gt;

&lt;p&gt;This post walks through how RexIDE was designed, the tradeoffs behind its architecture, and why a &lt;strong&gt;local first, execution centric&lt;/strong&gt; approach became the core principle.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building Persistent Terminal State
&lt;/h2&gt;

&lt;p&gt;The primary goal was simple:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Keep context alive, across projects, terminals, and AI agents, without forcing the developer to think about infrastructure.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That goal immediately shaped every technical decision that followed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Technical Tradeoffs of Local Execution
&lt;/h2&gt;

&lt;p&gt;One of the earliest decisions was whether AI execution should happen in the cloud or directly on the developer’s machine. Cloud models offer excellent quality, but they introduce friction through API keys and billing management, trust concerns around proprietary code, and a heavy dependency on latency and availability.&lt;/p&gt;

&lt;p&gt;Local models remove those concerns entirely. They keep code on the machine, work offline, and feel instant when integrated correctly.&lt;/p&gt;

&lt;p&gt;RexIDE was designed &lt;strong&gt;local first by default&lt;/strong&gt;, with the option to layer in cloud models only when the user explicitly opts in. Privacy and control are the baseline, not premium features.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Note on Codex and the Recent Shift
&lt;/h2&gt;

&lt;p&gt;Recently, OpenAI launched the Codex desktop app, which meaningfully validates the direction RexIDE took early on: local execution with persistent context.&lt;/p&gt;

&lt;p&gt;Codex today focuses on a single toolchain, the Codex ecosystem, and does a solid job at solving the local, long running AI workflow problem within that scope.&lt;/p&gt;

&lt;p&gt;RexIDE takes a broader approach. Instead of committing to a single AI provider or tool, it was designed from the start to act as an orchestrator for multiple local AI CLIs across platforms, including Claude Code, Codex CLI, and OpenCode. All of these run locally on macOS, Windows, and Linux, side by side, inside the same execution centric environment.&lt;/p&gt;

&lt;p&gt;This reflects how many developers already work today: using multiple AI tools side by side, depending on the task at hand. The environment should adapt to that reality rather than force consolidation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Model Selection and Resource Constraints
&lt;/h2&gt;

&lt;p&gt;Running AI models locally isn’t free: CPU, memory, and energy usage matter, especially on a machine you actively work on. RexIDE intentionally uses multiple layers of local AI execution. It utilizes external local CLIs such as Claude Code, Codex CLI, and similar tools for full reasoning and agent-driven workflows, while also employing embedded lightweight local models for smaller, fast tasks like snippet analysis, summarization, and structural understanding directly inside the app.&lt;/p&gt;

&lt;p&gt;Instead of chasing the largest model possible, RexIDE follows a simple rule:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Use the smallest model that reliably meets the task’s requirements.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Lightweight embedded models handle frequent, low-latency tasks without context switching, while heavier reasoning is delegated to specialized local CLIs that already excel at those workflows.&lt;/p&gt;

&lt;p&gt;Multiple model sizes were tested against real workflows including transcription, summarization, and code understanding while monitoring latency, sustained CPU usage, and memory pressure. The selected models stay well within acceptable resource bounds, ensuring they don’t interfere with compilers, editors, or other foreground tasks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Native PTY Execution and State Persistence
&lt;/h2&gt;

&lt;p&gt;Most IDEs optimize for editing, but RexIDE optimizes for execution. That means providing real terminals rather than simulated ones, maintaining long running processes that don’t reset when focus changes, and enabling AI agents that operate inside the same execution context as the developer.&lt;/p&gt;

&lt;p&gt;This approach eliminates a huge amount of mental overhead. You don’t restart tasks, re-explain context, or reconstruct state — everything stays alive.&lt;/p&gt;

&lt;h2&gt;
  
  
  Engineering Stateless Backend Boundaries
&lt;/h2&gt;

&lt;p&gt;RexIDE doesn’t require a backend to function, but it was designed with one in mind. If a backend were introduced, it would follow a few strict principles: stateless request handling, explicit separation between compute, user state, and storage, and strong session isolation to prevent data leakage.&lt;/p&gt;

&lt;p&gt;The client would remain the source of truth for execution context, with the backend acting only as an optional accelerator — never a dependency.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resource Management and Background Throttling
&lt;/h2&gt;

&lt;p&gt;Performance isn’t something you optimize later, it is a core part of the user experience. RexIDE treats system resources with respect by ensuring heavy work runs off the main thread and AI workloads throttle when the app is backgrounded.&lt;/p&gt;

&lt;p&gt;If the tool ever feels like it’s “in the way,” it has failed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reversible Architectural Decisions
&lt;/h2&gt;

&lt;p&gt;Early design decisions are rarely perfect. RexIDE was built with reversibility in mind.&lt;/p&gt;

&lt;p&gt;Short, time boxed prototypes were preferred over long debates. Decisions were explicitly labeled as reversible or irreversible, which made it easier to move fast without locking the project into bad paths. That mindset allowed rapid iteration without accumulating architectural debt.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Result
&lt;/h2&gt;

&lt;p&gt;RexIDE isn’t trying to be another editor with AI bolted on. It’s an execution environment where context persists, AI agents feel native, and the developer stays in control.&lt;/p&gt;

&lt;p&gt;Everything else is a consequence of that choice.&lt;/p&gt;

&lt;p&gt;If you’re building tools for developers today, the question isn’t whether to add AI — it’s &lt;strong&gt;where it lives, how much context it gets, and who ultimately controls it&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;RexIDE represents one way to approach that problem.&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>programming</category>
      <category>coding</category>
      <category>ai</category>
    </item>
    <item>
      <title>AWS Lambda Pricing 2026 Guide</title>
      <dc:creator>Tomer Ben David</dc:creator>
      <pubDate>Mon, 02 Feb 2026 13:16:55 +0000</pubDate>
      <link>https://dev.to/tomerbendavid/aws-lambda-pricing-2026-guide-5dnf</link>
      <guid>https://dev.to/tomerbendavid/aws-lambda-pricing-2026-guide-5dnf</guid>
      <description>&lt;p&gt;AWS Lambda is the "serverless" gold standard for a service that lets you run code without managing any servers. You only pay for what you use, but if you don't understand the rules, your bill can grow surprisingly fast.&lt;/p&gt;

&lt;p&gt;Here is everything you need to know about Lambda pricing in a clear, simple guide for 2026.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The Two Main Costs: Requests and Duration
&lt;/h2&gt;

&lt;p&gt;AWS calculates your bill using two primary factors:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Requests:&lt;/strong&gt; You are charged for the total number of times your functions start running.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Duration:&lt;/strong&gt; You are charged for the time it takes your code to execute, rounded to the nearest &lt;strong&gt;1 millisecond&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Free Tier (The Good News)
&lt;/h3&gt;

&lt;p&gt;Every month, AWS gives you &lt;strong&gt;1 million requests&lt;/strong&gt; and &lt;strong&gt;400,000 GB-seconds&lt;/strong&gt; of compute time for free. The best part? This free allowance never expires.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. The "Cold Start" Cost Shift (New for 2025)
&lt;/h2&gt;

&lt;p&gt;A "cold start" happens when Lambda has to set up a new environment to run your code. This used to be a performance problem; now it's a budget problem.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important Update:&lt;/strong&gt; As of August 2025, AWS now bills for the initialization (&lt;strong&gt;INIT&lt;/strong&gt;) phase of a cold start. Before this change, the setup time was mostly free. Now, it’s a recurring budget item, especially for heavy runtimes like &lt;strong&gt;Java&lt;/strong&gt; or &lt;strong&gt;C#&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Three Simple Ways to Save (Up to 34%)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Tip 1: Switch to ARM (Graviton2)
&lt;/h3&gt;

&lt;p&gt;Most Lambda functions run on x86 processors by default. However, switching to ARM-based Graviton2 processors can offer up to &lt;strong&gt;34% better price-performance&lt;/strong&gt; and costs roughly &lt;strong&gt;20% less per millisecond&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Tip 2: "Right-Size" Your Memory
&lt;/h3&gt;

&lt;p&gt;When you give your function more memory (RAM), AWS automatically gives it more CPU power.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Too little memory:&lt;/strong&gt; Your code runs so slowly that you end up paying &lt;em&gt;more&lt;/em&gt; in duration charges.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Too much memory:&lt;/strong&gt; You might give your code more CPU than it can actually use.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Pro Tip:&lt;/strong&gt; Use tools like &lt;strong&gt;AWS Lambda Power Tuning&lt;/strong&gt; to find the "sweet spot" where speed and cost intersect.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Tip 3: The "Lambda-Less" Approach
&lt;/h3&gt;

&lt;p&gt;The cheapest Lambda is the one you don't run. Many AWS services—like &lt;strong&gt;API Gateway&lt;/strong&gt;, &lt;strong&gt;AppSync&lt;/strong&gt;, and &lt;strong&gt;EventBridge Pipes&lt;/strong&gt;—can talk directly to databases (DynamoDB) or queues (SQS) without needing a Lambda function in the middle. This eliminates compute costs and reduces latency.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Pro-Tip: Don't Spend Money Waiting
&lt;/h2&gt;

&lt;p&gt;For complex, multi-step workflows that need to "wait" for something to happen, don't use Lambda to manage the wait. Use &lt;strong&gt;AWS Step Functions&lt;/strong&gt; instead. You don’t pay for the time Step Functions sits idle, whereas a Lambda function would bill you for every second it spends waiting.&lt;/p&gt;




&lt;h3&gt;
  
  
  Citations &amp;amp; Further Reading
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;a href="https://aws.amazon.com/lambda/pricing/" rel="noopener noreferrer"&gt;AWS Lambda Pricing Official Page&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://www.cloudzero.com/blog/lambda-pricing/" rel="noopener noreferrer"&gt;CloudZero: AWS Lambda Pricing Guide&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://edgedelta.com/company/knowledge-center/aws-lambda-cold-start-cost" rel="noopener noreferrer"&gt;EdgeDelta: Lambda Cold Start Costs&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://serverlessrepo.aws.amazon.com/applications/arn:aws:serverlessrepo:us-east-1:451282441545:applications~aws-lambda-power-tuning" rel="noopener noreferrer"&gt;AWS Lambda Power Tuning Tool&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://github.com/awslabs/llrt" rel="noopener noreferrer"&gt;LLRT: Low Latency Runtime for Lambda&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://aws.amazon.com/step-functions/pricing/" rel="noopener noreferrer"&gt;AWS Step Functions Pricing&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>aws</category>
      <category>programming</category>
      <category>cloud</category>
      <category>devops</category>
    </item>
    <item>
      <title>The Almost Correct System</title>
      <dc:creator>Tomer Ben David</dc:creator>
      <pubDate>Sun, 25 Jan 2026 00:00:00 +0000</pubDate>
      <link>https://dev.to/tomerbendavid/the-hidden-cost-of-almost-correct-systems-4hoa</link>
      <guid>https://dev.to/tomerbendavid/the-hidden-cost-of-almost-correct-systems-4hoa</guid>
      <description>&lt;p&gt;In modern service and cloud architectures, the most painful production failures aren’t usually caused by "bad code" in the traditional sense.&lt;/p&gt;

&lt;p&gt;They’re caused by &lt;strong&gt;good code making different assumptions.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is the reality of distributed systems. It’s uncomfortable to hear, especially if you’re a careful engineer who writes tests, handles errors, and thinks about edge cases. But once you see this pattern, you’ll start noticing it everywhere, from microservice outages to distributed deadlocks and system design interview questions.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The Baseline: Why "Working Code" != A Working System
&lt;/h2&gt;

&lt;p&gt;We naturally test the things we can control: the client, the API, the database. We run integration tests between them. If every individual component returns the correct output for a given input, we say the code is "correct."&lt;/p&gt;

&lt;p&gt;In a simple, local program, this is the ground truth. If every function is correct, the program is correct. But in a distributed cloud architecture, this logic breaks down. You can have three "correct" services that, when combined, create a catastrophic failure.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The failure isn’t usually inside your code, it’s in the space between your services.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Each component is built with &lt;strong&gt;assumptions&lt;/strong&gt; about how the rest of the system behaves. When those assumptions don’t match, the system becomes fragile, even if every line of code is technically perfect.&lt;/p&gt;

&lt;h2&gt;
  
  
  The assumption mismatch in practice
&lt;/h2&gt;

&lt;p&gt;Let’s look at something boring on purpose: &lt;strong&gt;timeouts&lt;/strong&gt;. Imagine this setup where every value looks reasonable on its own:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Client timeout:&lt;/strong&gt; 2 seconds&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Load balancer timeout:&lt;/strong&gt; 5 seconds&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Backend service timeout:&lt;/strong&gt; 30 seconds&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Database timeout:&lt;/strong&gt; No limit&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Step-by-Step Failure
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;The client sends a request.&lt;/li&gt;
&lt;li&gt;The backend is slow today (cold cache, lock contention, etc.).&lt;/li&gt;
&lt;li&gt;After &lt;strong&gt;2 seconds&lt;/strong&gt;, the client gives up and retries.&lt;/li&gt;
&lt;li&gt;The original request is &lt;strong&gt;still running&lt;/strong&gt; in the backend (it has 28 seconds left).&lt;/li&gt;
&lt;li&gt;Now the backend is doing the same work twice.&lt;/li&gt;
&lt;li&gt;The database sees double load. Latency increases further.&lt;/li&gt;
&lt;li&gt;More clients retry. The system spirals.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;No single component broke. The database didn't crash; the service didn't leak memory. The failure emerged from &lt;strong&gt;how their assumptions interacted.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Bridging the gap with explicit contracts
&lt;/h2&gt;

&lt;p&gt;Every boundary in a system has a &lt;strong&gt;contract&lt;/strong&gt;, whether you wrote it down or not. We often rely on implicit contracts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;"This request finishes quickly"&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;"Retries are safe"&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;"This operation runs once"&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The problem is that when assumptions are implicit, different parts of the system invent their own version of reality. That’s where "almost correct" systems are born.&lt;/p&gt;

&lt;p&gt;If a client times out at 2 seconds, the backend &lt;em&gt;must&lt;/em&gt; know its work is no longer wanted. If a client retries, the operation &lt;em&gt;must&lt;/em&gt; be idempotent.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to reason about boundaries
&lt;/h2&gt;

&lt;p&gt;To move from "Junior" to "Senior" systems thinking, you have to shift your primary question:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Junior-level thinking:&lt;/strong&gt; "Is my code correct?"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Senior-level thinking:&lt;/strong&gt; "What assumptions does my code make, and who depends on them?"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The longer a system lives, the more &lt;strong&gt;assumption drift&lt;/strong&gt; it accumulates. To combat this, you need to implement alignment strategies:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Align Timeouts:&lt;/strong&gt; Upstream timeouts should generally be &lt;em&gt;shorter&lt;/em&gt; than downstream ones only if you have aggressive retries; actually, a better pattern is &lt;strong&gt;Deadline Propagation&lt;/strong&gt;, where the remaining time budget is passed along the request chain.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Make Operations Idempotent:&lt;/strong&gt; If a caller assumes they can retry safely, you must assume they &lt;em&gt;will&lt;/em&gt; retry multiple times.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Use Backpressure:&lt;/strong&gt; If you assume the system can handle X load, you must have a way to say "no" when X is exceeded, rather than slowing down for everyone.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Why "almost correct" is worse than "broken"
&lt;/h2&gt;

&lt;p&gt;Failing loud is a feature. When a system crashes or returns a 500, you know exactly when and where it broke. Experienced engineers aim for this &lt;strong&gt;"fail fast"&lt;/strong&gt; behavior because it surfaces problems immediately.&lt;/p&gt;

&lt;p&gt;The danger comes from the impulse often seen in junior developers to "handle" every error by hiding it. This leads to the most dangerous state: the almost-correct system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Almost correct systems are quieter and more dangerous:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;They pass unit tests.&lt;/li&gt;
&lt;li&gt;They survive staging.&lt;/li&gt;
&lt;li&gt;They fail only under specific load.&lt;/li&gt;
&lt;li&gt;They fail only when timing is unlucky.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These failures are hard to reproduce because &lt;strong&gt;no single line of code is wrong.&lt;/strong&gt; This is why postmortems often sound like: &lt;em&gt;"Everything behaved as designed... just not together."&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  A systems thinking checklist
&lt;/h2&gt;

&lt;p&gt;When designing or reviewing a system, don’t start with implementation details. Start with &lt;strong&gt;failure questions&lt;/strong&gt; to force assumptions into the open:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[ ] &lt;strong&gt;Retries:&lt;/strong&gt; What retries this, and what is the retry budget?&lt;/li&gt;
&lt;li&gt;[ ] &lt;strong&gt;Timeouts:&lt;/strong&gt; Who times out first? Does the work stop when they do?&lt;/li&gt;
&lt;li&gt;[ ] &lt;strong&gt;Idempotency:&lt;/strong&gt; What happens if this exact request runs twice?&lt;/li&gt;
&lt;li&gt;[ ] &lt;strong&gt;Partial Failure:&lt;/strong&gt; What happens if the DB update succeeds but the cache update fails?&lt;/li&gt;
&lt;li&gt;[ ] &lt;strong&gt;State:&lt;/strong&gt; What state survives a crash, and what assumption does the &lt;em&gt;next&lt;/em&gt; run make about that state?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Closing Thought
&lt;/h2&gt;

&lt;p&gt;Great software isn’t built by eliminating bugs. It’s built by eliminating &lt;strong&gt;surprises&lt;/strong&gt;. These surprises don’t come from bad code; they come from assumptions that were never made explicit.&lt;/p&gt;




&lt;h3&gt;
  
  
  Citations &amp;amp; Further Reading
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://sre.google/sre-book/cascading-failures/" rel="noopener noreferrer"&gt;Google SRE Book: Chapter 22 - Addressing Cascading Failures&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://aws.amazon.com/message/5467D2/" rel="noopener noreferrer"&gt;Amazon DynamoDB 2015 Incident Post-mortem&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/No_Silver_Bullet" rel="noopener noreferrer"&gt;Fred Brooks: No Silver Bullet — Accident vs. Essence in Software Engineering&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.somethingsimilar.com/2013/01/14/notes-on-distributed-systems-for-young-bloods/" rel="noopener noreferrer"&gt;Notes on Distributed Systems for Young Bloods&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Originally published at &lt;a href="https://rex.mindmeld360.com" rel="noopener noreferrer"&gt;https://rex.mindmeld360.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>systemdesign</category>
      <category>architecure</category>
      <category>testing</category>
      <category>softwarequality</category>
    </item>
    <item>
      <title>Java Memory Model Deep Dive: Visibility, Reordering, and the Truth About Volatile</title>
      <dc:creator>Tomer Ben David</dc:creator>
      <pubDate>Wed, 21 Jan 2026 00:00:00 +0000</pubDate>
      <link>https://dev.to/tomerbendavid/java-memory-model-deep-dive-visibility-reordering-and-the-truth-about-volatile-58gd</link>
      <guid>https://dev.to/tomerbendavid/java-memory-model-deep-dive-visibility-reordering-and-the-truth-about-volatile-58gd</guid>
      <description>&lt;p&gt;In a single-threaded Java program, you are protected by a beautiful lie called &lt;strong&gt;as-if-serial semantics&lt;/strong&gt;. If you write &lt;code&gt;int x = 1; int y = 2;&lt;/code&gt;, the JVM and CPU can reorder those lines however they want to improve performance, but they promise that the &lt;em&gt;result&lt;/em&gt; will be exactly as if they ran in order. Inside that single thread, the reordering is invisible.&lt;/p&gt;

&lt;p&gt;As soon as you introduce a second thread, the lie falls apart. That second thread doesn't see the "as-if-serial" promise; it sees the raw memory as it updates. Code that looks perfectly logical can suddenly fail in ways that seem impossible. This is where the &lt;strong&gt;Java Memory Model (JMM)&lt;/strong&gt; comes in—it is the official "contract" that defines exactly when and how threads are allowed to see each other's changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The Core Problem: Performance over Predictability
&lt;/h2&gt;

&lt;p&gt;Most developers assume the JVM executes code exactly line-by-line as written. In reality, the JVM and your CPU are obsessed with speed. To run faster, they perform optimizations that create two main issues: &lt;strong&gt;Reordering&lt;/strong&gt; and &lt;strong&gt;Visibility&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reordering: The "Out of Order" Execution
&lt;/h3&gt;

&lt;p&gt;The compiler or the CPU might decide to swap two instructions if it thinks the final result will be the same.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// What you wrote:&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;flag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// What the CPU might actually execute:&lt;/span&gt;
&lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;flag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a single thread, this swap doesn't matter. But if another thread is waiting for &lt;code&gt;flag&lt;/code&gt; to be true so it can read &lt;code&gt;a&lt;/code&gt;, it might see &lt;code&gt;flag == true&lt;/code&gt; before &lt;code&gt;a&lt;/code&gt; has actually been set to 1.&lt;/p&gt;

&lt;h3&gt;
  
  
  Visibility: The Cache Problem
&lt;/h3&gt;

&lt;p&gt;Modern CPUs have their own local caches (L1, L2, L3). When a thread updates a variable, it might only save that change in its local CPU cache to save time. Other threads, running on different CPU cores, will continue to read the old value from their own caches or main memory. The change is "invisible" to them.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. The Solution: "Happens-Before" (HB)
&lt;/h2&gt;

&lt;p&gt;The JMM doesn't promise that everything will always be in order. Instead, it provides a set of rules called the &lt;strong&gt;Happens-Before&lt;/strong&gt; relationship. &lt;/p&gt;

&lt;p&gt;Think of Happens-Before as a "visibility bridge." If Action A happens-before Action B, then any change made by Action A is guaranteed to be visible to the thread performing Action B.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Most Important Rules:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Program Order:&lt;/strong&gt; In a single thread, every action happens-before any action that comes later in the code.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Volatile Variable Rule:&lt;/strong&gt; A write to a &lt;code&gt;volatile&lt;/code&gt; field happens-before every subsequent read of that same field. (This is the "signal" we use to bridge threads).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Monitor Lock Rule:&lt;/strong&gt; Releasing a lock (&lt;code&gt;synchronized&lt;/code&gt;) happens-before any subsequent acquisition of that same lock.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Thread Life Cycle:&lt;/strong&gt; Calling &lt;code&gt;thread.start()&lt;/code&gt; happens-before any action in that thread. All actions in a thread happen-before a successful &lt;code&gt;thread.join()&lt;/code&gt; on that thread.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  3. The &lt;code&gt;volatile&lt;/code&gt; Modifier: A Modern Guide
&lt;/h2&gt;

&lt;p&gt;A common mistake is thinking &lt;code&gt;volatile&lt;/code&gt; is just for "disabling caches." It's more powerful than that. &lt;/p&gt;

&lt;p&gt;When you write to a &lt;code&gt;volatile&lt;/code&gt; variable, the JVM ensures two things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Visibility:&lt;/strong&gt; The write is immediately flushed to main memory, and any subsequent read will pull the latest value.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Ordering (The Barrier):&lt;/strong&gt; The JVM prevents instructions from being reordered around the volatile read/write. It acts as a "memory barrier."&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  What &lt;code&gt;volatile&lt;/code&gt; does NOT do: Atomicity
&lt;/h3&gt;

&lt;p&gt;This is the biggest landmine in Java. &lt;code&gt;volatile&lt;/code&gt; does &lt;strong&gt;not&lt;/strong&gt; make compound operations atomic.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;volatile&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;increment&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="o"&gt;++;&lt;/span&gt; &lt;span class="c1"&gt;// NOT THREAD-SAFE&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;count++&lt;/code&gt; is actually three steps: read, add 1, write. If two threads do this at the same time, they might both read the same value, add 1, and write the same result back, losing one increment. For this, you need &lt;code&gt;AtomicInteger&lt;/code&gt; or &lt;code&gt;synchronized&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Unsafe Publication: Why "null" isn't always null
&lt;/h2&gt;

&lt;p&gt;One of the strangest bugs in Java is when a thread sees an object that is "half-initialized."&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Thread A&lt;/span&gt;
&lt;span class="n"&gt;shared&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Helper&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Thread B&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;shared&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;shared&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;x&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Could print 0 instead of 42!&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because of reordering, the CPU might assign the memory address of the new &lt;code&gt;Helper&lt;/code&gt; object to the &lt;code&gt;shared&lt;/code&gt; variable &lt;em&gt;before&lt;/em&gt; the constructor has finished setting &lt;code&gt;x = 42&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to fix this (Safe Publication):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Make the &lt;code&gt;shared&lt;/code&gt; field &lt;code&gt;volatile&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  Initialize it inside a &lt;code&gt;synchronized&lt;/code&gt; block.&lt;/li&gt;
&lt;li&gt;  Make the fields inside the object &lt;code&gt;final&lt;/code&gt;. The JMM gives special visibility guarantees to &lt;code&gt;final&lt;/code&gt; fields once the constructor finishes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5. Double-Checked Locking (DCL)
&lt;/h2&gt;

&lt;p&gt;The classic way to create a lazy singleton safely is the Double-Checked Locking pattern. It relies heavily on &lt;code&gt;volatile&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;volatile&lt;/span&gt; &lt;span class="nc"&gt;Resource&lt;/span&gt; &lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Resource&lt;/span&gt; &lt;span class="nf"&gt;getResource&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Resource&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// First check (no locking)&lt;/span&gt;
        &lt;span class="kd"&gt;synchronized&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// Second check (with locking)&lt;/span&gt;
                &lt;span class="n"&gt;resource&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note: We use the local variable &lt;code&gt;result&lt;/code&gt; to reduce the number of times we have to read the &lt;code&gt;volatile&lt;/code&gt; field, which is a small performance optimization.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Beyond Volatile: VarHandles (Java 9+)
&lt;/h2&gt;

&lt;p&gt;In modern Java, if you need even more control than &lt;code&gt;volatile&lt;/code&gt; provides, you can use the &lt;code&gt;VarHandle&lt;/code&gt; API. It allows you to choose exactly how much "strictness" you want:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Opaque:&lt;/strong&gt; Ensures the value isn't cached, but allows reordering.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Acquire/Release:&lt;/strong&gt; A lighter version of volatile that only enforces ordering in one direction.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Volatile:&lt;/strong&gt; The full-strength version we discussed.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Practical Checklist for Concurrent Code
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Is the variable shared?&lt;/strong&gt; If yes, it must be protected by &lt;code&gt;volatile&lt;/code&gt;, &lt;code&gt;Atomic&lt;/code&gt; classes, or a lock.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Are you doing more than a simple write?&lt;/strong&gt; If you are reading-then-writing (like &lt;code&gt;count++&lt;/code&gt;), &lt;code&gt;volatile&lt;/code&gt; is not enough. Use &lt;code&gt;AtomicInteger&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Is your object fully built?&lt;/strong&gt; Never let the &lt;code&gt;this&lt;/code&gt; reference "escape" from a constructor (e.g., by passing it to another thread) before the constructor is finished.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Can you use &lt;code&gt;final&lt;/code&gt;?&lt;/strong&gt; Always prefer &lt;code&gt;final&lt;/code&gt; fields. They are the simplest way to ensure thread-safety for data that doesn't change.&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Citations &amp;amp; Further Reading
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;a href="https://docs.oracle.com/javase/specs/jls/se17/html/jls-17.html" rel="noopener noreferrer"&gt;Java Language Specification, Chapter 17: Threads and Locks&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html" rel="noopener noreferrer"&gt;JSR-133 (Java Memory Model) FAQ&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://shipilev.net/blog/2016/close-encounters-of-jmm-kind/" rel="noopener noreferrer"&gt;Aleksey Shipilёv: JMM Pragmatics&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/invoke/VarHandle.html" rel="noopener noreferrer"&gt;VarHandle API Documentation&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Originally published at &lt;a href="https://rex.mindmeld360.com" rel="noopener noreferrer"&gt;https://rex.mindmeld360.com&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>java</category>
      <category>concurrency</category>
      <category>jvm</category>
      <category>multithreading</category>
    </item>
  </channel>
</rss>
