{"record":{"id":"34c708b82a9b5b06","repo":"krahets/hello-algo","slug":"queue-is-empty-34c708","errorCode":null,"errorMessage":"Queue is empty","messagePattern":"Queue is empty","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"en/codes/typescript/chapter_stack_and_queue/array_queue.ts","lineNumber":58,"sourceCode":"        // Add num to the rear of the queue\n        const rear = (this.front + this.queSize) % this.capacity;\n        // Front pointer moves one position backward\n        this.nums[rear] = num;\n        this.queSize++;\n    }\n\n    /* Dequeue */\n    pop(): number {\n        const num = this.peek();\n        // Move front pointer backward by one position, if it passes the tail, return to array head\n        this.front = (this.front + 1) % this.capacity;\n        this.queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    peek(): number {\n        if (this.isEmpty()) throw new Error('Queue is empty');\n        return this.nums[this.front];\n    }\n\n    /* Return Array */\n    toArray(): number[] {\n        // Elements enqueue\n        const arr = new Array(this.size);\n        for (let i = 0, j = this.front; i < this.size; i++, j++) {\n            arr[i] = this.nums[j % this.capacity];\n        }\n        return arr;\n    }\n}\n\n/* Driver Code */\n/* Access front of the queue element */\nconst capacity = 10;\nconst queue = new ArrayQueue(capacity);","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/en/codes/typescript/chapter_stack_and_queue/array_queue.ts#L40-L76","documentation":"Thrown by ArrayQueue.peek when the queue is empty. peek returns nums[front]; with queSize === 0 the front pointer points at no logical element, so the method throws. pop calls peek first and re-throws this error, so popping an empty queue surfaces the same message.","triggerScenarios":"Calling pop or peek before any push; dequeueing more items than were enqueued; concurrent producers/consumers where the consumer outruns production.","commonSituations":"Polling loops without an emptiness check; queue drained then peeked again; capacity/front desync after failed operations.","solutions":["Check isEmpty() (or size() === 0) before pop/peek.","Drain with while (!queue.isEmpty()).","Wrap pop in a helper returning undefined on empty."],"exampleFix":"// before\nconst v = queue.pop(); // throws if empty\n\n// after\nconst v = queue.isEmpty() ? undefined : queue.pop();","handlingStrategy":"validation","validationCode":"const v = queue.isEmpty() ? undefined : queue.pop();","typeGuard":"function hasElements(q) { return typeof q.isEmpty === 'function' && !q.isEmpty(); }","tryCatchPattern":"try { return queue.pop(); }\ncatch (e) { if (!/Queue is empty/.test(e.message)) throw e; return undefined; }","preventionTips":["Always check isEmpty() before pop/peek.","Drain with while (!queue.isEmpty()).","In producer/consumer setups, gate pops on size > 0."],"tags":["queue","typescript","validation","guard-clause","empty-state"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}