5101 - Design & Analysis of Algo
← 5101 - Design & Analysis of Algo
session: "5101-design-analysis-of-algo/2026-08-30 0000 DAA 30 Aug Lab"
title: "DAA 30 Aug Lab"
date: "2026-08-30 00:00"
duration: "2h 00m"
language: "en"
model: "deepseek-ai/DeepSeek-V4.1-Flash-1017"
parts: 3
transcript_chars: 61118
generated: "2026-11-23T23:33:35+05:30"
This lab session covers the queue data structure: its FIFO rule, the four queue types (linear/simple, circular, priority, double-ended), and synchronous vs. asynchronous tasks. Students solve two HackerRank problems — "Ticket Counter Q2" (nq, dq, front operations) and a printer job problem (add x, print, next) — using Python's collections.deque, and see a custom queue class written from scratch to show what deque does underneath. The class also covers input parsing with input().split(), Python's implicit typing, real-world queue applications, and course logistics (lab links, HackerRank naming, attendance, exam format).
nq, dq, front) in Python using collections.deque, including the empty-queue edge case.input().split() and zero-based indexing.__init__, enqueue, dequeue, and front.deque class before its operations can be used.A queue is a structure where the first element in is the first to go out — "queue always follows FIFO" (first in, first out); a stack follows LIFO (last in, first out). Front is the first element; rear is the last. In the visual example, people line up 1–7 and leave in that same order. This rule is the basis of every problem solved in class.
Elements leave from one side only. Its disadvantage is a memory-wasting problem, which motivated the circular queue. The Ticket Counter Q2 problem uses only a linear queue, so rear/last-element operations are not considered for now.
Introduced to address the linear queue's memory-wasting problem. Details were deferred to later classes.
High-priority items go out first; low-priority items are processed later. Used in operating systems where high-priority jobs go first and low-priority jobs last.
You can exit from either side ("double exit", "both of the side equivalent"). The four queue types exist because of different use cases.
A synchronous task depends on its previous task completing before it can complete. Asynchronous tasks can run in parallel — e.g., loading a UI/loading screen on the main thread while a requested service is fetched from a backend API.
collections.deque and pop leftdeque is a class in the collections library; pop left is a function defined in that class whose logic pops the element from the left side — no need to define it yourself. Other functions mentioned: pop right, append, pop, push (stack-like; the instructor could not recall all of them). Underneath, the queue in collections is a list (or array) with a wrapper on top making it a queue; a stack is similarly a wrapper on the same structure — "these data structures are inherited from that array."
Requires three operations: enqueue (add to back), dequeue (remove the first element), and front (view the first element). __init__ is the primary constructor, invoked when the object is created; return sends the value back to wherever the function was called from. The custom class was shown only to demonstrate how the deque function is written underneath — for assignments, only import from collections.
splitinput().split() splits by default on space (it can split on other characters, e.g., comma, if specified). The result is stored in an array accessed by zero-based index: nq 10 gives operation[0] = "nq" and operation[1] = "10", which is converted to int and appended. Inputs are read one at a time inside the loop, not all at once.
Python is data-type aware; unlike C/C++ where you must write int a = 10 or string b, Python does not require explicit type declaration. If you explicitly write int of s, you are defining the value as int, and passing a string gives a type error.
Bank applications/lists processed one by one; IRCTC ticket booking (first-come-first-served, with randomization calculated in milliseconds/fractions of a second to allocate tickets); threading (Python threading, Android Kotlin). Interview guidance: analyze whether a problem hints at a particular data structure — recognize what it looks like from the question.
1 ≤ n ≤ 1000 — n is the number of operations; used as the loop bound for reading operations.1 ≤ x ≤ 10^5 — x is the customer/job number, starting from 1; used to bound the values enqueued.dq or front operation, print the required result on a new line; no nq output is printed — used to match the required output format.n from 1 to 1000, and x up to 10 to the power 5 — same bounds as above, applied to add x.Sample 1 — Ticket Counter Q2: 8 operations: dq, nq 5, nq 15, front, dq, front, dq, front.
dq on an empty queue → prints empty.nq 5 → 5 inserted (no print).nq 15 → 15 inserted; queue now has 5 and 15 (no print).front → should print 5 (the instructor noted a discrepancy in the given sample output and apologized for the mistake, concluding the front value should be 5).dq → 5 is removed; only 15 remains.front → prints the front value.dq → dequeues the remaining element.front → queue is empty, so prints empty.Sample 2: 7 operations: nq 10, nq 20, nq 30, front, dq, front, dq.
front prints 10; dq removes 10; front prints 20; dq removes 20, leaving only 30.Deque indexing illustration: with elements 1 2 3 4 (indices 0–3), calling dq pops from the left, so the queue becomes 2 3 4; the front value printed is q[0].
Split illustration: nq 10 split on space gives operation[0] = "nq" and operation[1] = "10"; operation[1] is converted to int and appended.
Solution logic (as described):
1. from collections import deque (transcript says "dq").
2. Read n via input.
3. Create q = deque() — an object to use append/pop-left/front functionality.
4. for _ in range(n): read operation = input().split().
5. If operation[0] == "nq": take operation[1], convert to int, q.append(...) (appends to the last).
6. elif operation is dq: if q (non-empty, boolean check), pop left; else print empty.
7. elif operation is front: if q, print q[0]; else print empty.
Printer problem: jobs arriving as 101, 102, 103 are processed as 101, 102, 103. Sample input 0: six operations — add 1, add 2, 3 (as described in the transcript); print means printing and dequeuing side by side; next means printing without removing. Sample input 1: six operations — add 50 and 60, print two values (50 and 60), then print (removing 50), then next (printing 60).
Test case walkthrough (student's failing test case): no job, no job, 99, no job; next gives no job, print gives no job, add 99 then print gives 99, then print gives no job.
Custom queue example: queue 10, 20, 30 → front 10; enqueue 40 → 10, 20, 30, 40; dequeue → 20, 30, 40.
Roll number example (logistics): from the email ID, the part after the name/underscore up to the @ is the roll number, e.g. subi_pa2607mte76 → roll number pa2607mte76. Another profile identifier example: first name and roll number "krishna_pa2603".
Kumar Devi / lab access: the instructor opened the "tech test lab" link and shared it; students should check and report. The instructor has no Moodle administration access and cannot confirm video access — contact the CP (course program contact, emailed when applying for the course).
Sanjay — assignment visibility: assignments will not be visible on the Moodle portal; an Excel-safe link will be provided with HackerRank assignment links, deadlines, and submission links.
Harimon: can catch up on all classes by going through previous classes (may take some time).
Ramesh (asked 5–7 times) — why create a variable/object for deque: because deque is a class; an object is needed to use its operations (nq, dq, front); the class cannot be used directly.
Mohit — circular queue: how it works and how a double-ended queue relates to FIFO. Circular queue was not yet introduced; it will be covered in the next classes. Double-ended queue means exit from both sides. The four queue types exist due to different use cases (e.g., priority queue from OS job scheduling).
Abhishek Pal — operation = input().split(): inputs are taken one at a time inside the loop (not all at once), so each iteration reads one operation line and splits it into operation[0] (e.g., nq) and operation[1] (the value).
Sarada — input/output interleaving: why does the code print output after each input while the question shows all inputs then all outputs? The loop reads one operation per iteration; after n operations the loop exits. If a dq occurs at the start when the queue is empty, it prints empty. The sample input is given together but the code processes line by line.
Python experience: a student said they are not experienced in Python; the instructor suggested following the Campus X video.
Mayur Naran — split: input is taken as a string, then input().split() splits by default on spaces; the result is stored in an array, and the operation is accessed by zero-based index (index 0, index 1). The loop checks operation[0].
Doubt on the HackerRank problem statement — does dq print something related to the element dequeued? Yes, it is a print (output format).
Varun Kumar Agrawal — first lab, missed previous lectures: is the correct way to take values one by one rather than all at once, using one queue for operations (length equal to the first input element, e.g. 8, 5, or 6 operations) and another loop for actual operations? There are two ways, and this is the correct way (also referenced Saradha's earlier question).
Jitendra Kumar Verma — is there an operation for back/rear to print? The question only uses a linear queue, so rear/last element is not considered for now; circular queue, priority queue, and double-ended queue will be taught in the next class.
Prakash Chandraja — can we use multiple operators? Yes, append, pop left, print from the collection.
Ganesh Gupta — is pop left a function or something else? It is already defined in the collections library; the deque class has a function defined for pop left whose logic pops the element from the left side.
Anand Gupta — where can this concept be used and where did it originate? Real-world examples — bank applications, lists processed one by one, IRCTC ticketing (first-come-first-served with randomization in milliseconds), and threading.
Nehan Pera — anything else to import besides deque, since the name is confusing with the removal operation? There is only one class defining the queue concept, imported from collections; custom implementations are also shown in today's class. On other functions: pop right, append, pop, push exist; the instructor could not recall all of them.
Doubt on the custom dq function returning a value — how to return the stream/value popped: Python does not require explicit type declaration (unlike int a = 10 / string b in other languages); if you explicitly write int of s, passing a string gives a type error.
Aditya Kumar — is the queue in collections a list or array? It is a list (or array) with a wrapper on top making it a queue; a stack is similarly a wrapper on the same structure.
Kashish Satyarthi — one test case fails in question 2; will it be considered or must it be redone? It can be considered; the instructor walks through the no job / 99 / no job sequence to check input-output.
Sanjeev Kumar — can the same condition work if the input is text (a string)? Yes; input().split() is used, and you can customize the conditions.
Sanjeev Kumar — will linked lists be studied, since the order was linked list then stack and queue? The instructor refers to the syllabus and what he has instructed; in Python there is a list, so it is not here (as in Java/C++).
Rajiv Kumar — for the assignment, can we use collections or must we create the class? No need to make any class; only import from collections. The custom class was shown only to demonstrate how the deque function is written underneath.
Sudhansu Sarma — only two sample test cases visible in the first question; are hidden test cases missing? They were not put (no hidden test cases).
Usnik Nath — joined late, missed first two classes; how to access earlier assignments? The lab test 2 link is already opened; you can paste the current lab link (lab 3) and change 3 to 2; the first one will be opened and he will be informed.
Prushna Chandra Surdeo — missed initial classes; HackerRank configuration and naming convention? Go through the Moodle website and check the video recordings; naming convention — first your roll number then underscore your name; if not sufficient, roll number only; an already existing account is also okay, then change the name.
Subhi Sarma — attendance: is 75% mandatory, and does watching recorded videos on official Teams count? Yes, it is correct. Can attendance be shared with roll numbers? The instructor does not have the attendance list; check with the CP team — only the CP team tracks attendance. Will less than 75% attendance bar you from the main exam? The instructor does not know, as he has not seen any such cases; contact the CP team. Also asked how to fetch the roll number from the email ID (answered as above).
Silbra Tripathi — HackerRank submissions: problems show as solved via the direct link, but submissions/scores are not visible in the profile. You should be able to see it in your contest; the instructor does not know why it is not accessible.
Asushi — ticket counter q2 (30 marks): 133 students solved but none got a score. The instructor has the submission marks review, not in terms of score; there may be an error on his side (possibly in test cases) — he will take it according to the submission. (He also notes he cannot assign marks for it and does not know why.)
Queue check in the print operation: in the selection where operation equals print, the if q condition is checked and then pop left — is an explicit empty-queue check needed? No; the check is already being done on the queue (if q), and Python automatically checks in the background whether it has a value.
Late submission: will submitting the first assignment on 23 August (due 22 August) affect marks? No, it will not affect your marks.
Syllabus: the syllabus shown is the 2024 syllabus — is the same applicable for 2026? Not known; check with CEP.
Test labs: where are the links for Test Lab 1 and Test Lab 2? Apology — the Excel sheet was not sent yet; it will be prepared today and shared (will appear in the quiz/assignment section).
Test lab marks: will test labs be included in semester marks? Yes, they will be included.
Exam format: will the semester exam have only MCQ-type questions? Yes — MCQ, MSQ, and true/false; no theory.
Assignment verification: student submitted assignments day by day — can the instructor check whether they were submitted? The instructor will check and inform; submissions made before the deadline are visible, and submissions after the deadline cannot be made. The profile showing "missing detail 0 percent" is fine.
std id / sys library: the assignment says to use "std id only" for input and output — is it mandatory to use the same sys function / sys library (std in, std out), even when submitting in Python? [unclear] — the instructor asks why std is required if submitting in Python; the student says it is mentioned even when Python is selected. The answer is cut off in the transcript.
One line per segment, [hh:mm:ss] timestamp first.
loading…