{"id":794,"date":"2025-07-26T13:06:30","date_gmt":"2025-07-26T07:36:30","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=794"},"modified":"2025-07-26T13:06:34","modified_gmt":"2025-07-26T07:36:34","slug":"implement-queue-using-array","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/implement-queue-using-array\/","title":{"rendered":"Implement Queue Using Array: Complete Guide with Normal and Optimal Solutions"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"0-problem-statement\">Problem Statement<\/h2>\n\n\n\n<p>You need to implement a&nbsp;<strong>queue<\/strong>&nbsp;using an array (Python list works as an array here). The queue should support two main operations:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Push(x):<\/strong>\u00a0Insert element\u00a0<code>x<\/code>\u00a0at the rear (end) of the queue.<\/li>\n\n\n\n<li><strong>Pop():<\/strong>\u00a0Remove and return the front (first) element of the queue. If the queue is empty, return\u00a0<code>-1<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/www.geeksforgeeks.org\/problems\/implement-queue-using-array\/1\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-0645e88f-e405-4399-87e1-b3a8ab428ddf\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"true\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/implement-queue-using-array\/#0-problem-statement\" style=\"\">Problem Statement<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/implement-queue-using-array\/#1-what-is-a-queue\" style=\"\">What is a Queue?<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/implement-queue-using-array\/#2-solution-1-normal-approach-using-python-list-with-append-and-pop0\" style=\"\">Solution 1: Normal Approach (Using Python List with append and pop(0))<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/implement-queue-using-array\/#3-intuition\" style=\"\">Intuition<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/implement-queue-using-array\/#4-detailed-approach\" style=\"\">Detailed Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/implement-queue-using-array\/#5-code\" style=\"\">Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/implement-queue-using-array\/#6-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/implement-queue-using-array\/#7-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/implement-queue-using-array\/#8-simplifying-it\" style=\"\">Simplifying It<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/implement-queue-using-array\/#9-solution-2-optimal-approach-using-fixed-size-array-with-front-and-rear-pointers\" style=\"\">Solution 2: Optimal Approach (Using Fixed-Size Array with Front and Rear Pointers)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/implement-queue-using-array\/#10-intuition\" style=\"\">Intuition<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/implement-queue-using-array\/#11-detailed-approach\" style=\"\">Detailed Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/implement-queue-using-array\/#12-code\" style=\"\">Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/implement-queue-using-array\/#13-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/implement-queue-using-array\/#14-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/implement-queue-using-array\/#15-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/implement-queue-using-array\/#16-simplifying-it\" style=\"\">Simplifying It<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/implement-queue-using-array\/#17-summary\" style=\"\">Summary<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/implement-queue-using-array\/#18-key-takeaways\" style=\"\">Key Takeaways<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-what-is-a-queue\">What is a Queue?<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A\u00a0<strong>queue<\/strong>\u00a0is a simple data structure that works on FIFO (First In, First Out) principle.<\/li>\n\n\n\n<li>That means: the\u00a0<strong>first<\/strong>\u00a0item that was added (<strong>pushed<\/strong>) is the\u00a0<strong>first<\/strong>\u00a0item to be removed (<strong>popped<\/strong>).<\/li>\n<\/ul>\n\n\n\n<p><em>Think of a queue like a line of people waiting for a movie ticket: the first person in line (front) gets served first, and new people join at the end (rear).<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-solution-1-normal-approach-using-python-list-with-append-and-pop0\">Solution 1: Normal Approach (Using Python List with append and pop(0))<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-intuition\">Intuition<\/h3>\n\n\n\n<p>The simplest way is to use a Python list like a dynamic array. We add new elements to the end (using append, like the rear of the queue) and remove from the beginning (using pop(0), like the front). It&#8217;s like having a flexible line where people join at the back, but when someone leaves from the front, everyone else has to shift forward &#8211; which can be slow if the queue is long!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-detailed-approach\">Detailed Approach<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Initialization<\/strong>: Create an empty list\u00a0<code>self.arr<\/code>.<\/li>\n\n\n\n<li><strong>Push(x)<\/strong>: Append x to the end of the list (rear).<\/li>\n\n\n\n<li><strong>Pop()<\/strong>: If the list is empty, return -1. Otherwise, remove and return the first element (front) using pop(0).<\/li>\n\n\n\n<li><strong>Why Normal?<\/strong>: This is easy to code, but pop(0) shifts all elements, making it slow for large queues.<\/li>\n\n\n\n<li><strong>Edge Cases<\/strong>: Handle empty queue by checking len(self.arr) == 0.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-code\">Code<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class MyQueue:\n    def __init__(self):\n        self.arr = []  # Empty list for queue\n    \n    # Function to push an element x in a queue.\n    def push(self, x):\n        self.arr.append(x)  # Add to end (rear)\n    \n    # Function to pop an element from queue and return that element.\n    def pop(self):\n        if len(self.arr) == 0:\n            return -1  # Queue empty\n        e = self.arr.pop(0)  # Remove from start (front)\n        return e\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">MyQueue<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">__init__<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.arr = []  <\/span><span style=\"color: #6A9955\"># Empty list for queue<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\"># Function to push an element x in a queue.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">push<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">x<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.arr.append(x)  <\/span><span style=\"color: #6A9955\"># Add to end (rear)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\"># Function to pop an element from queue and return that element.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">pop<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.arr) == <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> -<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #6A9955\"># Queue empty<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        e = <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.arr.pop(<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">)  <\/span><span style=\"color: #6A9955\"># Remove from start (front)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> e<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-code-explanation\">Code Explanation<\/h3>\n\n\n\n<p>We use Python&#8217;s list as our queue storage.\u00a0<code>push<\/code>\u00a0adds to the end with\u00a0<code>append<\/code>\u00a0(O(1) time).\u00a0<code>pop<\/code>\u00a0checks if empty (return -1), else uses\u00a0<code>pop(0)<\/code>\u00a0to remove the first element (front). But\u00a0<code>pop(0)<\/code>\u00a0is O(n) because it shifts all remaining elements left by one position.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>push()<\/strong>: O(1) &#8211; Append is constant time<\/li>\n\n\n\n<li><strong>pop()<\/strong>: O(n) &#8211; pop(0) shifts n elements<\/li>\n\n\n\n<li><strong>Space<\/strong>: O(n) &#8211; For n elements in the list<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-simplifying-it\">Simplifying It<\/h3>\n\n\n\n<p>This is like a line where people join at the back quickly, but when the first person leaves, everyone else has to scoot forward one by one. Easy to set up, but gets slow with many people!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"9-solution-2-optimal-approach-using-fixed-size-array-with-front-and-rear-pointers\">Solution 2: Optimal Approach (Using Fixed-Size Array with Front and Rear Pointers)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"10-intuition\">Intuition<\/h3>\n\n\n\n<p>To make both push and pop fast (O(1)), we use a fixed-size array and two pointers:&nbsp;<code>front<\/code>&nbsp;(points to the start) and&nbsp;<code>rear<\/code>&nbsp;(points to the next empty spot at the end). We add at&nbsp;<code>rear<\/code>&nbsp;and remove from&nbsp;<code>front<\/code>, just moving the pointers without shifting elements. It&#8217;s like reserving seats in a row: people sit from left to right, and when someone leaves from the front, we just move the &#8220;front&#8221; marker right &#8211; no need to shift everyone!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-detailed-approach\">Detailed Approach<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Initialization<\/strong>: Create a fixed array of size 100005 (big enough), set\u00a0<code>front = 0<\/code>\u00a0and\u00a0<code>rear = 0<\/code>.<\/li>\n\n\n\n<li><strong>Push(x)<\/strong>: Put x at arr[rear], then increment rear (add to end).<\/li>\n\n\n\n<li><strong>Pop()<\/strong>: If front == rear (empty), return -1. Else, get arr[front], increment front, and return it (remove from start).<\/li>\n\n\n\n<li><strong>Why Optimal?<\/strong>: No shifting &#8211; just pointer moves, so O(1) for both operations.<\/li>\n\n\n\n<li><strong>Edge Cases<\/strong>: Empty when front == rear. (Note: This is a simple queue without wrapping around.)<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"12-code\">Code<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class MyQueue:\n    def __init__(self):\n        self.arr = [0] * 100005  # Fixed-size array\n        self.front = 0            # Pointer to front\n        self.rear = 0             # Pointer to next empty spot at rear\n    \n    # Function to push an element x in a queue.\n    def push(self, x):\n        self.arr[self.rear] = x   # Place at rear\n        self.rear += 1            # Move rear forward\n    \n    # Function to pop an element from queue and return that element.\n    def pop(self):\n        if self.front == self.rear:\n            return -1             # Queue empty\n        temp = self.arr[self.front]  # Get front element\n        self.front += 1           # Move front forward\n        return temp\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">MyQueue<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">__init__<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.arr = [<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">] * <\/span><span style=\"color: #B5CEA8\">100005<\/span><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #6A9955\"># Fixed-size array<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.front = <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #6A9955\"># Pointer to front<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.rear = <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">             <\/span><span style=\"color: #6A9955\"># Pointer to next empty spot at rear<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\"># Function to push an element x in a queue.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">push<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">x<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.arr[<\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.rear] = x   <\/span><span style=\"color: #6A9955\"># Place at rear<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.rear += <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #6A9955\"># Move rear forward<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\"># Function to pop an element from queue and return that element.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">pop<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.front == <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.rear:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> -<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">             <\/span><span style=\"color: #6A9955\"># Queue empty<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        temp = <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.arr[<\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.front]  <\/span><span style=\"color: #6A9955\"># Get front element<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.front += <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">           <\/span><span style=\"color: #6A9955\"># Move front forward<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> temp<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"13-code-explanation\">Code Explanation<\/h3>\n\n\n\n<p>We use a fixed array with&nbsp;<code>front<\/code>&nbsp;tracking the first element and&nbsp;<code>rear<\/code>&nbsp;tracking where to add next.&nbsp;<code>push<\/code>&nbsp;writes to arr[rear] and increments rear (O(1)).&nbsp;<code>pop<\/code>&nbsp;reads arr[front] and increments front (O(1)), without shifting. Empty when front catches up to rear. (Note: Array size is fixed to 100005 as per common constraints; it won&#8217;t overflow in typical use.)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"14-dry-run\">Dry Run<\/h3>\n\n\n\n<p>Let&#8217;s trace through operations:<\/p>\n\n\n\n<p><strong>Initialize: arr = [0,0,0,&#8230;], front=0, rear=0<\/strong><\/p>\n\n\n\n<p><strong>push(2):<\/strong>&nbsp;arr=2, rear=1 \u2192 arr=[2,0,0,&#8230;]<br><strong>push(3):<\/strong>&nbsp;arr<a rel=\"noreferrer noopener\" target=\"_blank\" href=\"https:\/\/www.geeksforgeeks.org\/problems\/implement-queue-using-array\/1\"><\/a>=3, rear=2 \u2192 arr=[2,3,0,&#8230;]<br><strong>push(5):<\/strong>&nbsp;arr=5, rear=3 \u2192 arr=[2,3,5,&#8230;]<\/p>\n\n\n\n<p><strong>pop():<\/strong>&nbsp;front=0 !=3, temp=2, front=1, return 2 (arr unchanged, but front moved)<br><strong>pop():<\/strong>&nbsp;front=1 !=3, temp=3, front=2, return 3<br><strong>pop():<\/strong>&nbsp;front=2 !=3, temp=5, front=3, return 5<br><strong>pop():<\/strong>&nbsp;front=3 ==3, return -1 (empty)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"15-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>push()<\/strong>: O(1) &#8211; Just assign and increment pointer<\/li>\n\n\n\n<li><strong>pop()<\/strong>: O(1) &#8211; Just increment pointer and return<\/li>\n\n\n\n<li><strong>Space<\/strong>: O(1) fixed (array size is constant 100005, doesn&#8217;t grow with n)<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"16-simplifying-it\">Simplifying It<\/h3>\n\n\n\n<p>This is like a fixed row of seats where you add people from left to right (increasing rear). When removing, you just say &#8220;the front person left&#8221; and move the front marker right &#8211; no one shifts seats. Super fast for both adding and removing!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"17-summary\">Summary<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Approach<\/th><th>Push Time<\/th><th>Pop Time<\/th><th>Space<\/th><th>Best For<\/th><\/tr><\/thead><tbody><tr><td>Normal (List with pop(0))<\/td><td>O(1)<\/td><td>O(n)<\/td><td>O(n)<\/td><td>Simple coding<\/td><\/tr><tr><td>Optimal (Pointers in Fixed Array)<\/td><td>O(1)<\/td><td>O(1)<\/td><td>O(1) fixed<\/td><td>Interviews &amp; Efficiency<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The&nbsp;<strong>optimal approach<\/strong>&nbsp;is better for speed (O(1) pop) and is what you&#8217;d implement in interviews to show you understand efficient queues. The normal way is fine for small queues but slows down with large ones due to shifting.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"18-key-takeaways\">Key Takeaways<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Queues are FIFO: First in, first out.<\/li>\n\n\n\n<li>Python lists can mimic queues, but pop(0) is slow &#8211; use pointers for efficiency.<\/li>\n\n\n\n<li>In interviews, mention why the pointer method is better (no shifting, O(1) operations).<\/li>\n\n\n\n<li>Practice: Try adding more features like checking if full (when rear reaches array end) or making it circular for reuse!<\/li>\n<\/ul>\n\n\n\n<p>This covers the basics, now you can implement queues easily!<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\" target=\"_blank\" rel=\"noreferrer noopener\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\" target=\"_blank\" rel=\"noreferrer noopener\">+91-9712928220<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Statement You need to implement a&nbsp;queue&nbsp;using an array (Python list works as an array here). The queue should support two main operations: Here&#8217;s the [Problem Link] to begin with. What is a Queue? Think of a queue like a line of people waiting for a movie ticket: the first person in line (front) gets<\/p>\n","protected":false},"author":1,"featured_media":796,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[8,37],"class_list":{"0":"post-794","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-beginner","9":"tag-easy","10":"tag-stack-and-queues"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/07\/implement-queue-using-array-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/794","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=794"}],"version-history":[{"count":1,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/794\/revisions"}],"predecessor-version":[{"id":797,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/794\/revisions\/797"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/796"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}