BCA
Arrays, lists, stacks, queues, hashing, trees, and graphs
Study map: BCA Semester 3 data structures foundation
A data structure organises information to support operations. Start with the workload: lookup, insertion, deletion, ordered traversal, or relationship search. The number of elements and frequency of operations matter. An abstract data type describes permitted behaviour; an implementation determines how that behaviour is achieved.
1. Choose structure around operations
A data structure organises information to support operations. Start with the workload: lookup, insertion, deletion, ordered traversal, or relationship search. The number of elements and frequency of operations matter. An abstract data type describes permitted behaviour; an implementation determines how that behaviour is achieved.
An array-like structure supports indexed access, while insertion in the middle may require movement. A linked structure stores connections between elements and has different access costs. Do not compare structures by one operation alone. Memory use, locality, implementation complexity, and required invariants also affect the decision.
Further reading: CS50 algorithms
2. Stacks, queues, and hashing
A stack retrieves the most recently added item first. A queue retrieves the earliest added item first. They model different requirements: undo history often resembles a stack, while ordinary service requests often resemble a queue. The implementation must support the required end operations efficiently.
Hashing maps keys to locations using a hash function. Collisions occur when distinct keys share a location and require handling. Average-case lookup can be efficient under appropriate assumptions, while worst-case and resizing behaviour differ. A hash table does not automatically maintain sorted order. Define the required semantics before relying on a language library.
Further reading: Python data structures
3. Trees and graphs
Trees model hierarchical relationships; graphs model more general connections. A binary search tree maintains an ordering invariant, but an unbalanced tree can become as deep as a list. Balanced arrangements aim to control height. Traversals visit nodes according to a defined rule.
Breadth-first search explores a graph in layers and can find shortest path lengths in an unweighted graph. Depth-first search explores along a path before backtracking. Track visited states when cycles exist. A tree traversal that assumes no cycles is not automatically safe for a general graph. The representation affects traversal cost and memory.
Further reading: MIT 6.006: graph representations and breadth-first search
4. Complexity and a meaningful lab
State the input size and distinguish average, worst, and amortised costs. Big-O is a bound, not a precise execution time. Compare alternatives using the operations actually needed. Benchmarks can supplement theoretical reasoning but depend on implementation, data, and environment.
A useful lab implements a stack and queue, tests empty behaviour, builds a small graph with a cycle, and traces BFS and DFS. Then explain invariants and operation costs. Testing only successful ordinary inputs leaves important behaviour unspecified. Correctness and suitability matter before premature optimisation.
Further reading: Python data structures
Applied business–computing case: reason, implement and review
Compare a list, a dictionary-like structure and a queue for an order workflow. A list can preserve sequence; a keyed structure can support lookup; a queue can represent work waiting for processing. Choose according to required operations, size, ordering and identity. A structure that is efficient for one operation may be inconvenient for another.
State assumptions behind complexity claims. Expected constant-time hash lookup depends on implementation and conditions; it does not mean every business operation is constant time. Keep domain correctness separate from the structure: removing a queued item should not erase its audit history. Test duplicate identities, empty structures and unusual orderings. The right data structure makes an algorithm practical, while records and policies determine whether the resulting workflow remains understandable and correct.
Transfer practice: queue order, keyed lookup and an unweighted route
Three requests arrive with IDs A, B and C. A FIFO queue processes A then B then C, while a stack removes C then B then A. A keyed mapping supports looking up B without adopting either processing order. Trace these behaviours separately; preserving a request's history is a business requirement beyond removing it from a waiting structure. A duplicate ID needs a policy, not silent replacement by whichever arrival was last.
For graph practice, use undirected edges A-B, A-C, B-D and C-D. Starting BFS at A and visiting neighbours alphabetically discovers B and C at distance one and D at distance two. Marking visited vertices prevents repeated exploration around the cycle A-B-D-C-A. The shortest-path claim concerns number of edges in this unweighted graph; different travel times require another model. A tree traversal assumption is insufficient for this graph.
An undergraduate should draw the queue, stack and BFS states. A postgraduate should choose representations for workloads combining lookup, cancellation and ordered processing, explaining invariants and implementation costs. Python's documented deque offers fast operations at both ends, but that property does not make arbitrary middle deletion free. Research learners could benchmark representative operations with specified sizes and distributions. Neither this trace nor Big-O alone establishes actual runtime, and no executable lab is claimed.
Study questions and answers
Why is a hash table unsuitable when sorted traversal is required by itself?
Hash placement does not generally impose sorted key order. An additional ordering structure or sorting step is needed.
When does BFS give shortest path lengths?
For an unweighted graph, using layer-based exploration and appropriate visited-state handling.
Extended practice questions
Compare a stack and a queue through business task examples.
A stack removes the most recently added item first, which can support a bounded undo sequence. A queue normally removes the earliest added item first, which can represent arrival order in a service process. The choice reflects the intended operation, not an automatic guarantee of fairness or correctness. Urgent cases or dependencies may require additional rules, and cancelling an item needs a defined identity and state transition. Computers implement the structure, but the organisation must decide what ordering means for people and what exceptions are legitimate.
Marking guide: LIFO/FIFO; examples; policy/exceptions; identity/state. Indicative allocation: reasoning 4; application or workings 3; assumptions and limits 3.
Marks: 10
A stack receives A, B and C, then pops once. A service queue starts with five items, receives three and completes two. Trace the results.
The stack pop removes C, the last item added, leaving A then B with B at the top. The queue count becomes five plus three minus two, or six items, assuming no cancellations, duplication or other status changes. A count alone does not reveal which items remain or their waiting time; identifiers and order must be preserved to answer those questions. These examples distinguish an operation trace from a volume balance. Use defined transitions so a completed or cancelled task is not accidentally left active or counted twice.
Marking guide: Cremoved/ABremain; sixqueueitems; identity/order; stateassumptions. Indicative allocation: reasoning 4; application or workings 3; assumptions and limits 3.
Marks: 10
Critique choosing a data structure without stating the required operations.
A structure is useful relative to operations such as lookup, insertion, ordering, deletion or traversal. A list suitable for a short display may become inefficient for repeated identifier lookup, while an indexed structure has maintenance and memory costs. Establish correctness, duplicate handling and concurrency requirements before comparing speed. Use representative sizes and workload patterns rather than one favourable example. The business problem also includes meaning: a fast lookup of the wrong identifier remains wrong. Treat structure selection as a scoped tradeoff supported by evidence, not a fashionable technology label.
Marking guide: Operationrequirements; maintenance/memory; correctness; representativeworkload; semantics. Indicative allocation: reasoning 4; application or workings 3; assumptions and limits 3.
Marks: 10
Design research on queue disciplines and service outcomes.
Compare clearly defined ordering rules, such as arrival order and a documented priority rule, using suitable comparable or simulated workloads. Measure waiting-time distributions, accepted completion and effects on different case groups. Preserve urgency and dependency definitions so results are interpretable. Examine starvation or repeated postponement rather than only average throughput. The contribution would explain how a computational ordering choice interacts with organisational priorities and human consequences. A simulation can illuminate assumptions, but it does not establish that the same outcomes will occur in a live service without further validation.
Marking guide: Defineddisciplines/workloads; distributions/groupconsequences; starvation; simulationlimits. Indicative allocation: reasoning 4; application or workings 3; assumptions and limits 3.
Marks: 10
A support desk promises first-in-first-out handling but stores pending cases in a stack. What behaviour may result?
A stack removes the most recently added item first, whereas a queue supports removing the earliest pending item first. Using a stack can repeatedly postpone older cases when new work keeps arriving. Choose a queue if the policy is genuinely FIFO, and define how priority cases, cancellations and reopened work alter the rule. A data structure supports an ordering policy but does not guarantee service capacity or fairness. Operations owns the policy and IT tests the trace, including whether duplicate submissions create multiple entries for the same case.
Marking guide: LIFO/FIFO; older-case impact; exceptions; capacity/fairness and duplicates. Indicative allocation:reasoning4;application or workings3;assumptions and limits3.
Marks: 10
Compare a hash-based lookup with sorted traversal for a product catalogue.
A hash-based structure can support efficient lookup by an agreed key under its implementation assumptions, but ordinary hash lookup does not provide the required sorted traversal. A sorted structure or an explicit sorting step serves ordered output. Define whether the workload prioritises exact lookup, range queries or ordered browsing, and examine memory and update costs. Expected constant-time lookup is not a guarantee for every operation or input distribution. Business identifiers and duplicate policies must be correct before either structure can produce a meaningful catalogue result.
Marking guide: Lookup versus order/ranges; workload/cost; expected versus guaranteed;identity. Indicative allocation:reasoning4;application or workings3;assumptions and limits3.
Marks: 10
Enqueue A, B, C into an empty FIFO queue, remove one, then enqueue D. Give the removal and remaining order.
The first removal returns A because it was enqueued earliest. The remaining queue is B, C, and adding D places it after them, producing B, C, D. A later removal would return B. This trace assumes standard FIFO operations without priorities, retries or cancellation. If an urgent case must bypass the order, that is a different service policy and should be explicit rather than implemented through an accidental data-structure change. Trace tests help verify ordering but do not establish that waiting times meet a business service target.
Marking guide: RemovedA;remainingB/C/D;FIFO assumptions;priority and service distinction. Indicative allocation:reasoning4;application or workings3;assumptions and limits3.
Marks: 10
Specify a meaningful test plan for a pending-work queue.
Test empty removal, a singleton, several enqueues, alternating operations and the expected order after each step. Define whether empty removal raises an error or returns a specified result. Check duplicate identifiers and cancellation according to the intended business contract rather than assuming the structure decides them. Measure representative workloads only after correctness is established. Operations reviews the ordering and exception policy; developers verify implementation behaviour. Concurrent producers and consumers require additional coordination evidence, so a single-threaded classroom trace should not be described as a dependable production work-distribution service.
Marking guide: Order/boundaries; duplicate/cancel contract; correctness before timing; concurrency limit. Indicative allocation:reasoning4;application or workings3;assumptions and limits3.
Marks: 10
Revision cards
Compare a stack and a queue through business task examples.
Stacks are last-in-first-out; queues are first-in-first-out. Business fairness and exceptions require separate ordering rules.
A stack receives A, B and C, then pops once. A service queue starts with five items, receives three and completes two. Trace the results.
Stack pop removes C, leaving A and B; queue balance is six items under the stated transitions.
A support desk promises first-in-first-out handling but stores pending cases in a stack. What behaviour may result?
A stack is last-in-first-out, which can delay older cases. FIFO work normally needs queue behaviour plus explicit exception rules.
Compare a hash-based lookup with sorted traversal for a product catalogue.
Hash lookup and sorted browsing answer different operation needs. Choose structure or sorting around the actual workload, not a universal 'fastest' claim.
Enqueue A, B, C into an empty FIFO queue, remove one, then enqueue D. Give the removal and remaining order.
Remove A; after adding D the queue is B, C, D. Urgency exceptions require an explicit policy, not an accidental ordering change.
Specify a meaningful test plan for a pending-work queue.
Test empty, singleton and interleaved operations with known order. Duplicates, cancellation and concurrency require explicit rules beyond FIFO.
When might a queue suit a service workflow?
A queue models ordered pending work under a stated service rule. Define priority, exceptions and capacity separately; first-in-first-out may be inappropriate for urgent or time-critical cases.
Why consider both access patterns and updates when choosing a structure?
Fast lookup may require extra memory or costly maintenance. Evaluate the actual workload, correctness needs and growth rather than selecting a structure from one operation’s complexity alone.
Practice task
Implement a stack and queue, trace BFS on a five-node cyclic graph, and compare array, linked-list, and hash-table operations for a specific workload.
Sources and further reading
- CS50 algorithms
Type: Free learning material; no paid certificate required
Use: Review search, sorting, and growth rates. - Python data structures
Type: Free learning material; no paid certificate required
Use: See free language examples of common containers; use only relevant sections. - MIT 6.006: breadth-first search
Type: Free archived undergraduate lecture and notes
Use: Verify graph representations, visited-state handling and shortest paths by number of edges; use other course lectures for DFS and trees.