Unit I: Linear Data Structures - Static and Linked Lists

Start here if you are new. This unit builds the vocabulary of algorithms and data structures, then moves from arrays to dynamic memory and linked lists.

11 hours 12 topic blocks 36 viva prompts
A0A1A2A3
10->25->40NULL

Introduction to Algorithms

A recipe for solving a computational problem.

  • Input: data given to the algorithm.
  • Output: answer produced by the algorithm.
  • Finiteness: it must stop after a limited number of steps.
  • Definiteness: every step must be unambiguous.
Think IPOF: Input, Processing, Output, Finish.

Algorithm Attributes and Design Techniques

How we describe and build algorithms.

  • Brute force tries straightforward possibilities.
  • Divide and conquer splits a problem into smaller similar problems.
  • Greedy chooses the best-looking immediate option.
  • Dynamic programming stores results of overlapping subproblems.
Design techniques are problem-solving patterns, not programming languages.

Time Space Trade Off

Use more memory to save time, or save memory and spend time.

  • Lookup tables store answers for reuse.
  • Caching saves repeated computation.
  • Sparse matrix representation saves memory but may make some operations more complex.
  • Sorting first may take time but makes later searching faster.
Extra memory can act like a shortcut.

Data Structures, Classification and Operations

Ways to organize data so operations become easier.

  • Primitive: int, char, float, pointer.
  • Linear: array, linked list, stack, queue.
  • Non-linear: tree, graph.
  • Static: size fixed before use, like a normal array.
Linear means one-after-another; non-linear means branching or network-like.

Growth of Functions and Master's Theorem

How running time grows when input grows.

  • O(1): constant time.
  • O(log n): logarithmic time.
  • O(n): linear time.
  • O(n log n): common for efficient sorting.
For Master Theorem, compare f(n) with n^(log_b a).

Arrays: 1D, 2D and Multi-Dimensional

Same-type elements stored in contiguous memory.

  • Index usually starts from 0 in C-like languages.
  • Random access is fast: O(1).
  • Insertion and deletion in the middle can be costly because shifting is needed.
  • 2D arrays can be stored in row-major or column-major order.
Array = apartment building: room number gives direct location.

Memory Representation and Address Calculation

How locations are calculated from base address and index.

  • Base address is the address of the first element.
  • Element size depends on data type.
  • In row-major order, rows are stored one after another.
  • In column-major order, columns are stored one after another.
Address = starting point + distance.

Sparse Matrices: Types and Representation

Matrices with mostly zero values.

  • Triplet form stores row, column, value.
  • Header row often stores rows, columns, and number of non-zero values.
  • Diagonal matrix, triangular matrix, symmetric matrix, and band matrix are common sparse-like types.
  • Sparse storage reduces space when non-zero elements are few.
Do not store silence; store only the notes that are played.

Dynamic Memory Allocation

Memory requested while the program is running.

  • Stack memory is automatic and limited.
  • Heap memory is manually managed in languages like C.
  • C functions include malloc, calloc, realloc, and free.
  • Dynamic allocation supports linked lists, dynamic stacks, dynamic queues, and trees.
Static is booking seats before the event; dynamic is adding chairs as people arrive.

Dynamic Memory versus Static Memory Allocation

Fixed size versus runtime flexibility.

  • Static allocation can waste memory if size is overestimated.
  • Dynamic allocation can cause memory leaks if not released.
  • Arrays are usually static; linked structures are dynamic.
  • Dynamic memory may be slower due to allocation overhead.
Static = fixed box; dynamic = adjustable box.

Linked List Types

Nodes connected by links instead of contiguous memory.

  • Singly linked list: each node points to next.
  • Doubly linked list: each node points to previous and next.
  • Header linked list: first node stores metadata or simplifies operations.
  • Circular linked list: last node points back to first.
A linked list is like a treasure hunt: each clue points to the next location.

Linked List Operations

Creation, insertion, deletion, modification, searching, sorting, reversing, merging.

  • Creation: allocate nodes and connect them.
  • Insertion: link new node at beginning, end, or position.
  • Deletion: adjust links and free removed node.
  • Modification: search a node and change its data.
In linked lists, order is made by links, not by memory addresses.

Flashcards

Flip the cards until the answer feels obvious.

Differentiation Tables

Fast contrasts for exams and viva answers.

Array vs Linked List

PointArrayLinked List
MemoryContiguous memoryNon-contiguous nodes connected by links
SizeUsually fixedCan grow or shrink dynamically
AccessDirect O(1) by indexSequential O(n)
Insertion/DeletionCostly in middle due to shiftingEfficient if position pointer is known
Extra SpaceNo link fieldNeeds pointer fields

Static vs Dynamic Memory Allocation

PointStaticDynamic
TimeBefore or at compile/load timeDuring runtime
SizeFixedFlexible
Memory AreaStack/static area commonlyHeap
RiskMay waste spaceMemory leak or dangling pointer
Exampleint A[100]malloc/new node

Linear Search vs Binary Search

PointLinear SearchBinary Search
Data RequirementSorted or unsortedSorted only
MethodCheck one by oneCheck middle and halve range
Worst TimeO(n)O(log n)
Best UseSmall or unsorted dataLarge sorted arrays
Linked ListWorks naturallyNot efficient without random access

Selection vs Bubble vs Insertion Sort

PointSelectionBubbleInsertion
Main IdeaSelect minimumAdjacent swapsInsert into sorted part
Best TimeO(n^2)O(n) with early stopO(n)
Worst TimeO(n^2)O(n^2)O(n^2)
StableUsually noYesYes
Good ForFew swapsTeaching basicsSmall or nearly sorted data

Merge Sort vs Quicksort

PointMerge SortQuicksort
TechniqueDivide and mergePartition around pivot
Average TimeO(n log n)O(n log n)
Worst TimeO(n log n)O(n^2)
SpaceO(n) for arraysO(log n) average stack
StabilityStable if implemented carefullyUsually not stable

Stack vs Queue

PointStackQueue
PrincipleLIFOFIFO
InsertionTopRear
DeletionTopFront
OperationsPush, pop, peekEnqueue, dequeue, front
ExampleUndo, recursionScheduling, BFS

Linear Queue vs Circular Queue vs Deque vs Priority Queue

PointLinear QueueCircular QueueDequePriority Queue
ShapeStraight lineRingBoth ends activePriority-based
DeletionFront onlyFront onlyFront or rearHighest/lowest priority
InsertionRear onlyRear with wrapFront or rearBy priority rule
Main BenefitSimpleReuses spaceFlexible endsImportant items first
ExamplePrint queueBufferSliding windowEmergency room

Chaining vs Double Hashing

PointChainingDouble Hashing
Collision HandlingStores collided keys in bucket/listFinds another table slot using second hash
MemoryExtra links/bucketsUses table slots
DeletionUsually simplerNeeds care with deleted markers
Load FactorCan exceed 1Must stay below 1
ClusteringLess sensitiveReduces clustering compared with linear probing

BFS vs DFS

PointBFSDFS
Data StructureQueueStack or recursion
OrderLevel by levelDepth first
Shortest PathYes for unweighted graphNot guaranteed
MemoryCan store many frontier nodesStores path/recursion depth
UsesShortest unweighted pathCycle detection, components

Prim vs Kruskal

PointPrimKruskal
GrowthOne tree growsForest joins components
ChoiceMinimum edge from tree to outsideMinimum global edge avoiding cycle
Needs SortingNot necessarily with priority queueYes, sort edges
Cycle CheckAvoided by tree/outside ruleUses union-find
Good ForDense graphs with matrixSparse graphs with edge list

Dijkstra vs Bellman-Ford

PointDijkstraBellman-Ford
ProblemSingle-source shortest pathSingle-source shortest path
Negative EdgesNot allowedAllowed
Negative Cycle DetectionNoYes
SpeedFaster with priority queueSlower
MethodGreedy nearest vertexRepeated relaxation of all edges

BST vs AVL Tree vs B-Tree

PointBSTAVL TreeB-Tree
BalanceMay be unbalancedStrictly height-balancedBalanced multi-way
ChildrenAt most 2At most 2Many children
Worst SearchO(n)O(log n)O(log n)
Main UseSimple ordered dataFast in-memory searchDatabases and disks
MaintenanceSimpleRotationsSplit/merge nodes

Quiz

1. What does the Master Theorem commonly solve?

Short Notes

Unit I: Introduction to Algorithms

  • A recipe for solving a computational problem.
  • Think IPOF: Input, Processing, Output, Finish.
  • Input: data given to the algorithm.
  • Output: answer produced by the algorithm.

Unit I: Algorithm Attributes and Design Techniques

  • How we describe and build algorithms.
  • Design techniques are problem-solving patterns, not programming languages.
  • Brute force tries straightforward possibilities.
  • Divide and conquer splits a problem into smaller similar problems.

Unit I: Time Space Trade Off

  • Use more memory to save time, or save memory and spend time.
  • Extra memory can act like a shortcut.
  • Lookup tables store answers for reuse.
  • Caching saves repeated computation.

Unit I: Data Structures, Classification and Operations

  • Ways to organize data so operations become easier.
  • Linear means one-after-another; non-linear means branching or network-like.
  • Primitive: int, char, float, pointer.
  • Linear: array, linked list, stack, queue.

Unit I: Growth of Functions and Master's Theorem

  • How running time grows when input grows.
  • For Master Theorem, compare f(n) with n^(log_b a).
  • O(1): constant time.
  • O(log n): logarithmic time.

Unit I: Arrays: 1D, 2D and Multi-Dimensional

  • Same-type elements stored in contiguous memory.
  • Array = apartment building: room number gives direct location.
  • Index usually starts from 0 in C-like languages.
  • Random access is fast: O(1).

Unit I: Memory Representation and Address Calculation

  • How locations are calculated from base address and index.
  • Address = starting point + distance.
  • Base address is the address of the first element.
  • Element size depends on data type.

Unit I: Sparse Matrices: Types and Representation

  • Matrices with mostly zero values.
  • Do not store silence; store only the notes that are played.
  • Triplet form stores row, column, value.
  • Header row often stores rows, columns, and number of non-zero values.

Unit I: Dynamic Memory Allocation

  • Memory requested while the program is running.
  • Static is booking seats before the event; dynamic is adding chairs as people arrive.
  • Stack memory is automatic and limited.
  • Heap memory is manually managed in languages like C.

Unit I: Dynamic Memory versus Static Memory Allocation

  • Fixed size versus runtime flexibility.
  • Static = fixed box; dynamic = adjustable box.
  • Static allocation can waste memory if size is overestimated.
  • Dynamic allocation can cause memory leaks if not released.

Unit I: Linked List Types

  • Nodes connected by links instead of contiguous memory.
  • A linked list is like a treasure hunt: each clue points to the next location.
  • Singly linked list: each node points to next.
  • Doubly linked list: each node points to previous and next.

Unit I: Linked List Operations

  • Creation, insertion, deletion, modification, searching, sorting, reversing, merging.
  • In linked lists, order is made by links, not by memory addresses.
  • Creation: allocate nodes and connect them.
  • Insertion: link new node at beginning, end, or position.

Viva Question Bank

Introduction to Algorithms

  • What is an algorithm?
  • Why should an algorithm be finite?
  • What is the difference between an algorithm and a program?

Algorithm Attributes and Design Techniques

  • Name any three algorithm design techniques.
  • Where is divide and conquer used?
  • What does greedy choice mean?

Time Space Trade Off

  • Define time-space trade off.
  • Give one example where extra memory reduces time.
  • Why is sparse matrix storage a space-saving method?

Data Structures, Classification and Operations

  • What is a data structure?
  • Classify arrays and graphs.
  • List common operations on data structures.

Growth of Functions and Master's Theorem

  • What is Big O notation?
  • What is the recurrence of merge sort?
  • State the basic idea of Master's Theorem.

Arrays: 1D, 2D and Multi-Dimensional

  • What is an array?
  • Why is array access O(1)?
  • What is row-major order?

Memory Representation and Address Calculation

  • What is base address?
  • Differentiate row-major and column-major order.
  • Calculate address when base, index, and element size are given.

Sparse Matrices: Types and Representation

  • What is a sparse matrix?
  • What is triplet representation?
  • Name types of sparse matrices.

Dynamic Memory Allocation

  • What is dynamic memory allocation?
  • Differentiate malloc and calloc.
  • Why must dynamically allocated memory be freed?

Dynamic Memory versus Static Memory Allocation

  • Differentiate static and dynamic memory allocation.
  • What is a memory leak?
  • Why is dynamic memory more flexible?

Linked List Types

  • What is a linked list?
  • Differentiate singly and doubly linked lists.
  • What is a circular linked list?

Linked List Operations

  • What is the time complexity of searching in a linked list?
  • How do you insert a node after a given node?
  • Why is pointer order important during deletion?