{"id":99,"date":"2025-05-23T01:41:13","date_gmt":"2025-05-23T01:41:13","guid":{"rendered":"https:\/\/www.alerainfotech.com\/?p=99"},"modified":"2025-05-23T01:48:44","modified_gmt":"2025-05-23T01:48:44","slug":"mastering-deque-and-the-sliding-window-technique-in-python-with-visuals-real-life-examples-and-code","status":"publish","type":"post","link":"https:\/\/www.alerainfotech.com\/home\/2025\/05\/23\/mastering-deque-and-the-sliding-window-technique-in-python-with-visuals-real-life-examples-and-code\/","title":{"rendered":"Mastering\u00a0deque\u00a0and the Sliding Window Technique in Python \u2014 With Visuals, Real-Life Examples, and Code"},"content":{"rendered":"\n<p>Whether you&#8217;re preparing for interviews or building real-world systems, mastering the&nbsp;<code>deque<\/code>&nbsp;data structure and the&nbsp;<strong>sliding window technique<\/strong>&nbsp;will help you solve a class of problems quickly and efficiently.<\/p>\n\n\n\n<p>This post will walk you through:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u2705 What is a&nbsp;<code>deque<\/code><\/li>\n\n\n\n<li>\u2705 How it differs from&nbsp;<code>list<\/code><\/li>\n\n\n\n<li>\u2705 What is the sliding window technique<\/li>\n\n\n\n<li>\u2705 Real-life analogies, visuals, and code<\/li>\n\n\n\n<li>\u2705 The kinds of questions interviewers ask \u2014 and what answers they expect<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd37 What is&nbsp;<code>deque<\/code>?<\/h2>\n\n\n\n<p><code>deque<\/code>&nbsp;stands for&nbsp;<strong>double-ended queue<\/strong>, and it lives in Python\u2019s&nbsp;<code>collections<\/code>&nbsp;module. It allows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Adding\/removing elements from both ends<\/strong><\/li>\n\n\n\n<li><strong>Constant-time operations<\/strong>&nbsp;at either end<\/li>\n\n\n\n<li>A much better choice than&nbsp;<code>list<\/code>&nbsp;for&nbsp;<strong>queue-like<\/strong>&nbsp;or&nbsp;<strong>sliding window<\/strong>&nbsp;problems<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 Import and usage:<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>from collections import deque<br><br>dq = deque()<br>dq.append(1)         # [1]<br>dq.appendleft(0)     # [0, 1]<br>dq.pop()             # [0]<br>dq.popleft()         # []<br><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\uddf1 Visualizing a&nbsp;<code>deque<\/code><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Left End                          Right End<br>|                                   |<br>\u2b05 appendleft(x)   [A, B, C]   append(x) \u27a1<br>\u27a1 popleft()                     pop() \u2b05<br><\/code><\/pre>\n\n\n\n<p>You can think of it like a&nbsp;<strong>tunnel<\/strong>&nbsp;or&nbsp;<strong>conveyor belt<\/strong>&nbsp;where things can move in and out from either direction.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc Real-Life Examples<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Real Life<\/th><th><code>deque<\/code>&nbsp;Analogy<\/th><\/tr><\/thead><tbody><tr><td>Train with two doors<\/td><td>People board\/exit from either side<\/td><\/tr><tr><td>Call center ticket queue<\/td><td>Oldest ticket first (FIFO)<\/td><\/tr><tr><td>Undo\/Redo feature<\/td><td>Push\/pop from opposite ends<\/td><\/tr><tr><td>Temperature buffer<\/td><td>Remove oldest, add latest<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u274c Why&nbsp;<code>list<\/code>&nbsp;Can Be a Problem<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Operation<\/th><th><code>list<\/code><\/th><th><code>deque<\/code><\/th><\/tr><\/thead><tbody><tr><td><code>append()<\/code><\/td><td>\u2705 O(1)<\/td><td>\u2705 O(1)<\/td><\/tr><tr><td><code>pop()<\/code><\/td><td>\u2705 O(1)<\/td><td>\u2705 O(1)<\/td><\/tr><tr><td><code>insert(0, x)<\/code><\/td><td>\u274c O(n)<\/td><td>\u2705 O(1) (<code>appendleft<\/code>)<\/td><\/tr><tr><td><code>pop(0)<\/code><\/td><td>\u274c O(n)<\/td><td>\u2705 O(1) (<code>popleft<\/code>)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>So if you&#8217;re using&nbsp;<code>list.pop(0)<\/code>&nbsp;or&nbsp;<code>list.insert(0, x)<\/code>, you&#8217;re losing performance \u2014&nbsp;<code>deque<\/code>&nbsp;solves this.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2705 The Sliding Window Technique (With Visuals and Examples)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd0d What Is It?<\/h3>\n\n\n\n<p>The&nbsp;<strong>sliding window<\/strong>&nbsp;technique is used when you want to examine a&nbsp;<strong>subarray<\/strong>&nbsp;or&nbsp;<strong>substring<\/strong>&nbsp;of size&nbsp;<code>k<\/code>, and move that &#8220;window&#8221; across the full input&nbsp;<strong>without recomputing everything<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2753 Common Questions Interviewers Ask<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>&#8220;How do you find the max sum of subarray of size k?&#8221;<\/li>\n\n\n\n<li>&#8220;How do you find the longest substring without repeating characters?&#8221;<\/li>\n\n\n\n<li>&#8220;How would you process a live stream of numbers for moving max\/min?&#8221;<\/li>\n<\/ul>\n\n\n\n<p>\u2705 They\u2019re all sliding window problems.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u26a0\ufe0f Brute-Force Solution (Slow)<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>nums = [1, 3, 5, 2, 8, 1]<br>k = 3<br>max_sum = 0<br>for i in range(len(nums) - k + 1):<br>    max_sum = max(max_sum, sum(nums[i:i+k]))<br><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u274c Time:&nbsp;<code>O(n * k)<\/code><\/li>\n\n\n\n<li>You&#8217;re recalculating the sum&nbsp;<strong>each time<\/strong><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 Efficient Sliding Window (O(n) time)<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>def max_sum_k(nums, k):<br>    window_sum = sum(nums[:k])<br>    max_sum = window_sum<br>    for i in range(k, len(nums)):<br>        window_sum += nums[i] - nums[i - k]<br>        max_sum = max(max_sum, window_sum)<br>    return max_sum<br><\/code><\/pre>\n\n\n\n<p>\ud83d\udca1 Here\u2019s how it works:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You maintain the sum of a moving window<\/li>\n\n\n\n<li>When the window slides forward:\n<ul class=\"wp-block-list\">\n<li><strong>Subtract the value leaving<\/strong><\/li>\n\n\n\n<li><strong>Add the value entering<\/strong><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83e\uddf1 Visual Explanation<\/h3>\n\n\n\n<p>Example:&nbsp;<code>nums = [1, 3, 5, 2, 8, 1], k = 3<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Window 1: [1, 3, 5] \u2192 sum = 9<br>Window 2:    [3, 5, 2] \u2192 sum = 10 (add 2, remove 1)<br>Window 3:       [5, 2, 8] \u2192 sum = 15 (add 8, remove 3)<br>Window 4:          [2, 8, 1] \u2192 sum = 11<br><\/code><\/pre>\n\n\n\n<p>You move forward one index at a time,&nbsp;<strong>reusing work<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd25 Advanced Problem: Sliding Window Maximum<\/h2>\n\n\n\n<p><strong>Problem:<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Find the max in every window of size&nbsp;<code>k<\/code><br><code>nums = [1,3,-1,-3,5,3,6,7], k = 3<\/code><br>Output:&nbsp;<code>[3, 3, 5, 5, 6, 7]<\/code><\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 Interview-Ready Answer Using&nbsp;<code>deque<\/code><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>from collections import deque<br><br>def max_sliding_window(nums, k):<br>    dq = deque()<br>    result = []<br><br>    for i in range(len(nums)):<br>        # Remove out-of-window indices<br>        while dq and dq[0] &lt;= i - k:<br>            dq.popleft()<br>        <br>        # Remove smaller elements from the end<br>        while dq and nums[dq[-1]] &lt; nums[i]:<br>            dq.pop()<br>        <br>        dq.append(i)<br><br>        if i &gt;= k - 1:<br>            result.append(nums[dq[0]])<br><br>    return result<br><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd0d Step-by-Step Visual Explanation<\/h2>\n\n\n\n<p>Let&#8217;s walk through the input&nbsp;<code>nums = [1, 3, -1, -3, 5, 3, 6, 7]<\/code>&nbsp;with&nbsp;<code>k = 3<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd39 Initial State:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>i = 0<\/code>: nums[0] =\u00a0<strong>1<\/strong><\/li>\n\n\n\n<li><code>deque<\/code>:\u00a0<code>[0]<\/code>\u00a0\u2192 stores index<\/li>\n\n\n\n<li>No result yet (window not full)<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd39 i = 1 \u2192 nums[1] =&nbsp;<strong>3<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Compare\u00a0<code>nums[1] > nums[0] \u2192 3 > 1<\/code>, so remove index\u00a0<code>0<\/code><br>(we remove smaller values from back)<\/li>\n\n\n\n<li>Add index\u00a0<code>1<\/code><\/li>\n\n\n\n<li><code>deque<\/code>:\u00a0<code>[1]<\/code><\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd39 i = 2 \u2192 nums[2] =&nbsp;<strong>-1<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>-1 &lt; 3<\/code>, keep\u00a0<code>1<\/code><\/li>\n\n\n\n<li>Add index\u00a0<code>2<\/code><\/li>\n\n\n\n<li><code>deque<\/code>:\u00a0<code>[1, 2]<\/code><\/li>\n\n\n\n<li>Now\u00a0<code>i >= k-1<\/code>, so result \u2192\u00a0<code>nums[1] = 3<\/code><\/li>\n<\/ol>\n\n\n\n<p>\u2705 First window [1, 3, -1] \u2192 max =&nbsp;<strong>3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd39 i = 3 \u2192 nums[3] =&nbsp;<strong>-3<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Index\u00a0<code>1<\/code>\u00a0is in range \u2192 keep it<\/li>\n\n\n\n<li><code>-3 &lt; -1<\/code>, keep<\/li>\n\n\n\n<li>Add index\u00a0<code>3<\/code><\/li>\n\n\n\n<li><code>deque<\/code>:\u00a0<code>[1, 2, 3]<\/code><\/li>\n\n\n\n<li>Result \u2192\u00a0<code>nums[1] = 3<\/code><\/li>\n<\/ol>\n\n\n\n<p>\u2705 Window [3, -1, -3] \u2192 max =&nbsp;<strong>3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd39 i = 4 \u2192 nums[4] =&nbsp;<strong>5<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Remove index\u00a0<code>1<\/code>\u00a0(out of window:\u00a0<code>1 &lt;= 4-3<\/code>)<\/li>\n\n\n\n<li>Remove index\u00a0<code>3<\/code>\u00a0(nums[3] = -3 &lt; 5)<\/li>\n\n\n\n<li>Remove index\u00a0<code>2<\/code>\u00a0(nums[2] = -1 &lt; 5)<\/li>\n\n\n\n<li><code>deque<\/code>\u00a0becomes empty<\/li>\n\n\n\n<li>Add index\u00a0<code>4<\/code><\/li>\n\n\n\n<li><code>deque<\/code>:\u00a0<code>[4]<\/code><\/li>\n\n\n\n<li>Result \u2192\u00a0<code>nums[4] = 5<\/code><\/li>\n<\/ol>\n\n\n\n<p>\u2705 Window [-1, -3, 5] \u2192 max =&nbsp;<strong>5<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd39 i = 5 \u2192 nums[5] =&nbsp;<strong>3<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Keep index\u00a0<code>4<\/code>\u00a0(5 > 3)<\/li>\n\n\n\n<li>Add index\u00a0<code>5<\/code><\/li>\n\n\n\n<li><code>deque<\/code>:\u00a0<code>[4, 5]<\/code><\/li>\n\n\n\n<li>Result \u2192\u00a0<code>nums[4] = 5<\/code><\/li>\n<\/ol>\n\n\n\n<p>\u2705 Window [-3, 5, 3] \u2192 max =&nbsp;<strong>5<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd39 i = 6 \u2192 nums[6] =&nbsp;<strong>6<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Remove index\u00a0<code>4<\/code>\u00a0and\u00a0<code>5<\/code>\u00a0(both nums &lt; 6)<\/li>\n\n\n\n<li><code>deque<\/code>: []<\/li>\n\n\n\n<li>Add index\u00a0<code>6<\/code><\/li>\n\n\n\n<li><code>deque<\/code>:\u00a0<code>[6]<\/code><\/li>\n\n\n\n<li>Result \u2192\u00a0<code>nums[6] = 6<\/code><\/li>\n<\/ol>\n\n\n\n<p>\u2705 Window [5, 3, 6] \u2192 max =&nbsp;<strong>6<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd39 i = 7 \u2192 nums[7] =&nbsp;<strong>7<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Remove index\u00a0<code>6<\/code>\u00a0(6 &lt; 7)<\/li>\n\n\n\n<li>Add index\u00a0<code>7<\/code><\/li>\n\n\n\n<li><code>deque<\/code>:\u00a0<code>[7]<\/code><\/li>\n\n\n\n<li>Result \u2192\u00a0<code>nums[7] = 7<\/code><\/li>\n<\/ol>\n\n\n\n<p>\u2705 Window [3, 6, 7] \u2192 max =&nbsp;<strong>7<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2705 Final Result:<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">pythonCopyEdit<code>[3, 3, 5, 5, 6, 7]\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udd14 Why Use&nbsp;<code>deque<\/code>?<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Tracks only\u00a0<strong>relevant indices<\/strong><\/li>\n\n\n\n<li>Removes\u00a0<strong>outdated<\/strong>\u00a0and\u00a0<strong>smaller<\/strong>\u00a0values<\/li>\n\n\n\n<li>Always gives max at the\u00a0<strong>front<\/strong><\/li>\n\n\n\n<li>\u2705 Makes entire algorithm\u00a0<strong>O(n)<\/strong>\u00a0instead of O(nk)<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2705 Summary Diagram (Text Form)<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>  nums[i]   deque (indices)     Max Value<br>0     1         [0]               -<br>1     3         [1]               -<br>2    -1         [1, 2]            3<br>3    -3         [1, 2, 3]         3<br>4     5         [4]               5<br>5     3         [4, 5]            5<br>6     6         [6]               6<br>7     7         [7]               7<br><\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd0e Explanation:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>dq<\/code>&nbsp;stores&nbsp;<strong>indices<\/strong>, not values<\/li>\n\n\n\n<li>The&nbsp;<strong>max value is always at dq[0]<\/strong><\/li>\n\n\n\n<li>We remove:\n<ul class=\"wp-block-list\">\n<li>Outdated indices (outside the window)<\/li>\n\n\n\n<li>Smaller elements that don\u2019t help<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 Time:&nbsp;<code>O(n)<\/code><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 Space:&nbsp;<code>O(k)<\/code><\/h3>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2705 Real-Life Analogy of Sliding Window<\/h2>\n\n\n\n<p>Imagine you\u2019re looking out a moving bus window:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You see&nbsp;<strong>3 people<\/strong>&nbsp;walking down the sidewalk (your window size)<\/li>\n\n\n\n<li>Every second, 1 person leaves your view and 1 new person enters<\/li>\n\n\n\n<li>You only need to&nbsp;<strong>update<\/strong>&nbsp;what you already saw, not&nbsp;<strong>start over<\/strong><\/li>\n<\/ul>\n\n\n\n<p>That\u2019s the sliding window.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc When to Use&nbsp;<code>deque<\/code>&nbsp;for Sliding Windows<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Scenario<\/th><th>Why&nbsp;<code>deque<\/code>&nbsp;Helps<\/th><\/tr><\/thead><tbody><tr><td>Moving max\/min in stream<\/td><td>O(1) head\/tail updates<\/td><\/tr><tr><td>Temperature readings over time<\/td><td>Drop old, add new<\/td><\/tr><tr><td>Log monitoring<\/td><td>Real-time event window<\/td><\/tr><tr><td>Competitive programming<\/td><td>Clean + optimal<\/td><\/tr><tr><td>Interview problems<\/td><td>\u2705 Best-practice technique<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2705 Summary<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Feature<\/th><th>Benefit<\/th><\/tr><\/thead><tbody><tr><td><code>deque<\/code><\/td><td>Fast O(1) pop\/append both ends<\/td><\/tr><tr><td>Sliding window<\/td><td>Efficient O(n) moving logic<\/td><\/tr><tr><td>Ideal for<\/td><td>Queues, stacks, stream buffers<\/td><\/tr><tr><td>Avoid&nbsp;<code>list.pop(0)<\/code><\/td><td>Use&nbsp;<code>deque.popleft()<\/code>&nbsp;instead<\/td><\/tr><tr><td>Interview essential<\/td><td>Shows algorithmic thinking<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udfc1 Final Thoughts<\/h2>\n\n\n\n<p>\u2705&nbsp;<code>deque<\/code>&nbsp;is your best friend for:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Real-time queues<\/li>\n\n\n\n<li>Palindromes<\/li>\n\n\n\n<li>Undo\/redo systems<\/li>\n\n\n\n<li><strong>And sliding window problems<\/strong>&nbsp;like max, min, and stream analysis<\/li>\n<\/ul>\n\n\n\n<p>\u2705 The&nbsp;<strong>sliding window<\/strong>&nbsp;pattern helps you avoid brute force and write&nbsp;<code>O(n)<\/code>&nbsp;solutions for otherwise slow problems.<\/p>\n\n\n\n<p>Next time you&#8217;re tempted to use&nbsp;<code>list.pop(0)<\/code>, remember:&nbsp;<code>deque.popleft()<\/code>&nbsp;is the real hero.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Whether you&#8217;re preparing for interviews or building real-world systems, mastering the&nbsp;deque&nbsp;data structure and the&nbsp;sliding window technique&nbsp;will help you solve a class of problems quickly and efficiently. This post will walk you through: \ud83d\udd37 What is&nbsp;deque? deque&nbsp;stands for&nbsp;double-ended queue, and it lives in Python\u2019s&nbsp;collections&nbsp;module. It allows: \u2705 Import and usage: from collections import dequedq = deque()dq.append(1) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-99","post","type-post","status-publish","format-standard","hentry","category-python-blog"],"_links":{"self":[{"href":"https:\/\/www.alerainfotech.com\/home\/wp-json\/wp\/v2\/posts\/99","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.alerainfotech.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.alerainfotech.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.alerainfotech.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.alerainfotech.com\/home\/wp-json\/wp\/v2\/comments?post=99"}],"version-history":[{"count":3,"href":"https:\/\/www.alerainfotech.com\/home\/wp-json\/wp\/v2\/posts\/99\/revisions"}],"predecessor-version":[{"id":102,"href":"https:\/\/www.alerainfotech.com\/home\/wp-json\/wp\/v2\/posts\/99\/revisions\/102"}],"wp:attachment":[{"href":"https:\/\/www.alerainfotech.com\/home\/wp-json\/wp\/v2\/media?parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.alerainfotech.com\/home\/wp-json\/wp\/v2\/categories?post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.alerainfotech.com\/home\/wp-json\/wp\/v2\/tags?post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}