{"record":{"id":"5767fd0266c1d13c","repo":"krahets/hello-algo","slug":"error-5767fd","errorCode":null,"errorMessage":"очередь пуста","messagePattern":"очередь пуста","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ru/codes/typescript/chapter_stack_and_queue/array_queue.ts","lineNumber":58,"sourceCode":"        // С помощью операции взятия по модулю вернуть rear к началу после выхода за конец массива\n        const rear = (this.front + this.queSize) % this.capacity;\n        // Добавить num в хвост очереди\n        this.nums[rear] = num;\n        this.queSize++;\n    }\n\n    /* Извлечь из очереди */\n    pop(): number {\n        const num = this.peek();\n        // Указатель head сдвигается на одну позицию назад; если он выходит за конец, то возвращается в начало массива\n        this.front = (this.front + 1) % this.capacity;\n        this.queSize--;\n        return num;\n    }\n\n    /* Доступ к элементу в начале очереди */\n    peek(): number {\n        if (this.isEmpty()) throw new Error('очередь пуста');\n        return this.nums[this.front];\n    }\n\n    /* Вернуть Array */\n    toArray(): number[] {\n        // Преобразовывать только элементы списка в пределах фактической длины\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/* Инициализация очереди */\nconst capacity = 10;\nconst queue = new ArrayQueue(capacity);","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/ru/codes/typescript/chapter_stack_and_queue/array_queue.ts#L40-L76","documentation":"Thrown by peek() on an array-backed circular queue when isEmpty() is true. The message ('очередь пуста', Russian for 'queue is empty') guards the read nums[front]. Because pop() calls peek() first, the throw also propagates from pop() on an empty queue.","triggerScenarios":"Calling peek() or pop() on a queue with queSize === 0; dequeuing in a loop that exceeds the number of enqueued elements; off-by-one in a producer/consumer where the consumer outruns the producer.","commonSituations":"BFS/level-order traversal that pops after the queue is drained; ring-buffer consumers that assume capacity implies available elements.","solutions":["Check queue.isEmpty() before peek()/pop().","Drive the consumer loop with while (!queue.isEmpty()).","When capacity and size are confused, verify queSize rather than capacity before popping.","Catch the error if an empty queue is an acceptable runtime condition."],"exampleFix":"// before\nconst x = queue.pop(); // throws if empty\n\n// after\nwhile (!queue.isEmpty()) {\n    const x = queue.pop();\n}","handlingStrategy":"validation","validationCode":"if (!queue.isEmpty()) {\n    const head = queue.peek();\n}","typeGuard":null,"tryCatchPattern":"try {\n    const head = queue.peek();\n} catch (e) {\n    if (e instanceof Error && e.message === 'очередь пуста') {\n        // queue empty; handle gracefully\n    } else throw e;\n}","preventionTips":["Check isEmpty() before peek()/pop().","Distinguish queSize from capacity; only queSize reflects available elements.","Drive consumers with while (!queue.isEmpty())."],"tags":["queue","typescript","empty-state","circular-array","validation"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}