5101 - Design & Analysis of Algo
← 5101 - Design & Analysis of Algo
session: "5101-design-analysis-of-algo/2026-10-11 0000 DAA 11 Oct lab"
title: "DAA 11 Oct lab"
date: "2026-10-11 00:00"
duration: "1h 36m"
language: "en"
model: "deepseek-ai/DeepSeek-V4.1-Flash-1017"
parts: 2
transcript_chars: 43411
generated: "2026-11-23T23:55:56+05:30"
This lab covered two distinct topics. First, the Longest Common Prefix string problem: an initial hash-map/counting approach was considered and rejected in favour of an optimal approach that takes the first string as the initial prefix and shrinks it character-by-character against every other string. Second, an introduction to trees: the non-linear hierarchical data structure, its terminology (node, root, leaf, edge), the types of binary trees (binary, full, complete, perfect, skewed), and implementing a tree node in Python. Traversals (in-order, pre-order, post-order) and BFS/DFS were previewed and deferred to the next class.
strs[0] and shrinking it via column-wise character comparison against each remaining string.Node class with data, left and right attributes and construct a small tree by assignment.Given an array of strings, the output is the longest prefix shared by all strings; if no string matches even a single letter, the expected output is the empty string. This is the LeetCode-style string problem that anchors the first half of the class. Example input used in class: flower, flow, flight — the common prefix is fl.
A hash-map / counting approach was considered and rejected as the solution strategy. The instructor flagged that they will touch on the hash-map approach again later (see Follow-ups).
Take strs[0] (string zero, e.g. flower) as the initial prefix. Compare it character-by-character (column-wise) against every other string; on a mismatch, break and replace the prefix with prefix[0:i] (the part that matched so far). Because the prefix only gets shorter as more strings are processed, this is efficient. This is the core algorithm of the class and the basis of the dry run below.
Comparing characters position by position across strings is sufficient; splitting strings into characters first is not necessary. The column-wise comparison works even for long strings/sentences (the constraint allows lengths up to 200, which covers sentences), and strings can be passed directly for processing.
F and f are not equal, so a comparison can fail purely due to case. Converting all strings to lowercase (e.g. s.lower() for s in strs) before comparing avoids this. This connects directly to the prefix-shrinking algorithm, where a case mismatch would incorrectly shrink the prefix.
Characters that are not human-readable (broken Unicode, byte-order issues) can appear when data comes from text files, PDFs, data correction, or machine-generated information. Such data must be encoded/decoded (e.g. into UTF-8) to be handled. This was raised by a student in the context of NLP/streaming and connects to the problem's constraint of English letters only.
A tree is a non-linear data structure used to organize data hierarchically; unlike an array where elements are arranged in a sequence, a tree organizes elements into parent-child relationships (e.g. 1 is a parent of 2, 2 is a parent of 4). A middle-layer node can be both a parent and a child. This begins the data-structure half of the class.
class Node with a constructor (def __init__) taking self and data; self.data = data; left and right children initialized to None. The class is a blueprint for creating a tree node; __init__ runs whenever a new node is created. This is the implementation bridge from tree theory to code.
Pre-order traversal was explained with the "three sticks" analogy — put sticks at each node (left, middle/position, right); for pre-order, go with the left stick and traverse till the end. On the example tree this yields 10, 20, 4, then 5, then 30, 6, 7 (as described). BFS/DFS are mostly used in graphs, for root-searching or path-finding problems; level order traversal is BFS-related. All of this is deferred to the next class.
i < len(prefix) and i < len(s) — both conditions must hold; if either fails, the loop stops. Used to bound the comparison so neither string is indexed out of range.prefix[i] != s[i], then break. Used to stop comparing as soon as the prefix no longer matches.prefix = prefix[0:i] (i.e. characters up to index i). Used to shrink the prefix to the part that matched so far.len("flower") = 6, len("flow") = 4 — lengths used in the dry run to determine where the loop condition fails.n = 2^{h+1} - 1, where h is the height. Used to count nodes in a perfect binary tree.n - 1 (number of nodes minus one); time to reach the last node: O(n). Used to show a skewed tree degenerates to linked-list behaviour.Dry run with prefix = "flower", s = "flow":
i = 0: prefix[0] = f, s[0] = f → matchi = 1: l vs l → matchi = 2: o vs o → matchi = 3: w vs w → matchi = 4: check 4 < 6 (true) and 4 < 4 (false) → loop condition fails, loop stopsprefix = prefix[0:4] = flowNext iteration with prefix = "flow", s = "flight":
i = 0: f vs f → matchi = 1: l vs l → matchi = 2: o vs i → mismatch → breakprefix = prefix[0:2] = flflCase-sensitivity example: comparing flight with a string starting with capital F fails at the first character because capital F ≠ lowercase f; fix by converting all strings to lowercase first.
Perfect binary tree: depths 0, 1, 2 → height = 2. Applying the formula: 2^(2+1) − 1 = 2^3 − 1 = 7 total nodes.
Binary tree example: root 10 with children 20 and 30; node 20 has only a left child — still a valid binary tree.
Python construction walkthrough:
root = Node(20) → constructor runs, self.data = 20, left and right are None.root.left = Node(10), root.right = Node(30) → 20 now has left child 10 and right child 30.root.left.left = Node(40), root.right.right = Node(50) → extends the tree further.root.data, root.left, root.right shows the single node's contents initially.Complete binary tree discussion: with nodes D, E, F filled left to right there is no gap; leaving a gap on the left (e.g. filling C's right while leaving something on the left unfilled) makes it invalid. A tree with nodes 5 and 6 at the same level but not satisfying the other conditions is not perfect; the instructor called such a case "partial" / "invalid as complete."
Test case 4 of the coding problem was reported failing for one student but passing for Satya.
Q (Anuja): Why not split the strings into characters beforehand instead of comparing column-wise?
A: Splitting is not necessary. The column-wise character comparison works even for long strings/sentences (the constraint allows lengths up to 200, which covers sentences). Splitting is not required for processing — you can pass the strings directly.
Q: What about capital vs lowercase letters?
A: Capital F and lowercase f are not the same, so the comparison fails. You can convert the strings to lowercase (e.g. s.lower() for s in strs) before comparing.
Q: What about junk values / special characters in a sentence?
A: Human-readable characters are fine; the concern is machine-language kind of characters. When data comes from a text file or PDF and is put into streaming, you may encounter such characters — you can check for them during processing or handle them beforehand. In data correction or machine-generated information, there can be broken Unicode (byte-order issues); such data must be encoded/decoded into UTF-8 characters. These are not normal characters. (The instructor noted they could share an example of such symbolic characters later.)
Q (Anuja, via instructor's paraphrase): The problem statement has a constraint that only English characters are used and a length is assigned, so the UTF-8 concern is valid.
A: Yes, the point is valid; we have to work within that constraint. The instructor had already mentioned "English letters only" in the constraints and apologized if something was missed. He noted he doesn't have the constraint text at hand; one could convert to UTF-8 or decode to Latin characters if needed.
Q: Test case 4 is failing.
A: It passes for Satya, so the instructor said he might have a mistake and would check on his end.
Q (on the node code): When you do root.left = Node(10), what gets assigned to left — is 10 assigned to left?
A: Node(10) creates an instance of the node class; the constructor passes 10 to self.data, and that object is assigned to the left attribute. Initially left/right are None; assigning Node(10) replaces None with the node object whose data is 10 and whose own left/right are None. The instructor walked through this step by step (with some back-and-forth) until the student said "got it."
Q (Vikas): Complete binary tree and full binary tree look the same, because in a complete binary tree any section also has 0 or 2 children?
A: No — the complete tree example is only "partially complete"; the rule is that the last level must be filled left to right with no gaps. If you traverse left to right there should be no gap (e.g. D, E, F with no gap). If you leave a gap on the left and fill to the right, it is not complete.
Q: In full binary there are 0 or 2 nodes, and in perfect binary there are exactly 2 nodes and leaf nodes at the same depth; if I have 5 and 6 at the same level, why is it not perfect?
A: Because the other conditions (every internal node exactly two children, all leaves at the same depth) must hold together; the instructor confirmed the student's understanding ("then I understood").
Q: Is a given tree partial/incomplete?
A: It is invalid as a complete binary tree; "partial" is not a standard term here — the instructor said "partial you can say" but it is invalid as complete.
Q (from the morning class discussion): Are in-order/pre-order/post-order the BFS and depth-first algorithms you are going to cover?
A: These concepts are mostly used in graphs; DFS and BFS are used for root-searching or path-finding problems. Traversals (in-order, pre-order, post-order, level order) and BFS/DFS will be introduced in the next class.
Q: Resource question.
A: The instructor said he would search and send them, noted that in class you don't have to be diplomatic — just ask — and that educational resources are open/permissible. He mentioned following resources on Udemy and that CampusX is also good, though it focuses more on CNN-type topics.
prefix = strs[0], compare column-wise against each string, and on mismatch set prefix = prefix[0:i]; return the empty string if nothing matches.i < len(prefix) and i < len(s); if either fails, the loop stops.flower, flow, flight: after flow the prefix is flow; after flight it is fl; final answer fl.F ≠ f; convert with s.lower() before comparing.n = 2^{h+1} - 1; for height 2, n = 7.n − 1 and behaves like a linked list, with O(n) time to reach the last node.Node(10) creates an object whose data is 10 and whose left/right are None; assigning it to root.left replaces None with that object.1. Trace the optimal Longest Common Prefix algorithm on ["flower", "flow", "flight"], showing the prefix after each string.
Outline: prefix = flower; compare with flow — matches at i = 0..3, loop stops at i = 4 because 4 < 4 is false; prefix = prefix[0:4] = flow. Compare with flight — matches f, l, mismatches at i = 2 (o vs i), break; prefix = prefix[0:2] = fl. Return fl.
2. Why is splitting the strings into characters unnecessary, and what constraint makes column-wise comparison sufficient?
Outline: column-wise comparison indexes both strings at position i directly; the constraint allows string length up to 200, which covers sentences; strings can be passed directly for processing.
3. Distinguish full, complete and perfect binary trees, with one invalid example for each rule.
Outline: full — every node has 0 or 2 children (a node with exactly one child violates it); complete — all levels filled except the last, filled left to right with no gaps (a right child present while the left is absent violates it); perfect — every internal node has exactly two children and all leaves at the same depth (nodes 5 and 6 at the same level without the other conditions is not perfect).
4. A perfect binary tree has height 2. How many nodes does it have? Show the formula.
Outline: n = 2^{h+1} − 1 with h = 2 → 2^3 − 1 = 7 nodes.
5. Write a Python Node class for a binary tree and construct a root 20 with left child 10 and right child 30. Explain what root.left = Node(10) assigns.
Outline: class Node with def __init__(self, data): self.data = data, self.left = None, self.right = None. Then root = Node(20), root.left = Node(10), root.right = Node(30). Node(10) creates an instance whose data is 10 and whose left/right
One line per segment, [hh:mm:ss] timestamp first.
loading…