0% found this document useful (0 votes)
15 views3 pages

Search Algorithms and Data Structures Guide

Uploaded by

Jidnyasa Kor
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Search Algorithms and Data Structures Guide

Uploaded by

Jidnyasa Kor
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Search Techniques (Linear & Binary Search)


Q1. What is linear search?
A. Checks each element one by one until found.

Q2. What is binary search?


A. Works on sorted lists and divides list to find element quickly.

Q3. Why must list be sorted for binary search?


A. Because it depends on comparing smaller or larger values.

Q4. Time complexity of linear search?


A. O(n)

Q5. Time complexity of binary search?


A. O(log n)

Q6. What does enumerate() do?


A. Gives index and value during looping.

Q7. If element not found?


A. Returns -1 and prints “ID not found.”

2. Undo/Redo System using Stack


Q1. What is a stack?
A. A data structure that works on LIFO (Last In, First Out).

Q2. What is LIFO?


A. Last inserted element is removed first.

Q3. Why two stacks here?


A. One for Undo, one for Redo.

Q4. What does append() and pop() do?


A. append() adds, pop() removes top element.

Q5. Why clear redo stack after new change?


A. Because redo history becomes invalid.

Q6. Real life example?


A. Undo/Redo in text editor or browser history.
3. Student Record Management System (Doubly
Linked List)
Q1. What is doubly linked list?
A. Each node links to next and previous node.

Q2. What is a node?


A. Structure holding data and pointers.

Q3. Difference between singly & doubly linked list?


A. Singly points to next only, doubly points to next & prev.

Q4. How to add a student?


A. Create new node and link it at end.

Q5. How to delete a record?


A. Find roll number and adjust links.

Q6. How to search a record?


A. Traverse list until match found.

Q7. Advantage of linked list over array?


A. Dynamic memory and easy insertion/deletion.

4. Hash Table using Division Method & Linear


Probing
Q1. What is hash table?
A. Data structure storing key-value pairs using hash function.

Q2. What is hash function?


A. Converts key into index.

Q3. What is division method?


A. index = key % size.

Q4. What is collision?


A. Two keys map to same index.

Q5. What is linear probing?


A. Move to next index until empty slot found.
Q6. What if table full?
A. Shows “Hash table is full.”

Q7. Time complexity?


A. Average O(1), Worst O(n).

5. Binary Search Tree (BST)


Q1. What is BST?
A. Tree where left child < root < right child.

Q2. Main operations?


A. Insert, Search, Delete, Traverse.

Q3. What is inorder traversal?


A. Left → Root → Right (sorted order).

Q4. How is insertion done?


A. Compare key with root, move left/right until empty.

Q5. What is inorder successor?


A. Smallest node in right subtree.

Q6. Delete node with two children?


A. Replace with inorder successor.

Q7. Time complexity?


A. Average O(log n), Worst O(n).

Common questions

Powered by AI

A doubly linked list offers the key advantage of being able to traverse both forwards and backwards, which simplifies insertion and deletion operations at both ends or in the middle of the list compared to a singly linked list. This bidirectional capability can enhance the flexibility and efficiency of managing student records where frequent backward navigation might be necessary .

A binary search tree (BST) facilitates efficient searching with an average time complexity of O(log n) because each comparison enables a binary decision to be made, effectively halving the search space. However, performance can degrade to O(n) in the worst case if the tree becomes unbalanced, resembling a linear structure similar to a linked list, leading to inefficient search operations .

The time complexity of linear search is O(n), meaning it requires a number of steps proportional to the size of the dataset. In contrast, binary search, with a time complexity of O(log n), is significantly faster for large datasets. This is because binary search efficiently reduces the problem size by half with each step, making it superior in performance when dealing with sorted data .

The division method computes an index using the remainder of the division of a key by the table size, but this can result in collisions, where multiple keys map to the same index. Linear probing resolves these collisions by sequentially searching for the next available slot in the array. By placing collided elements adjacently, it maintains occupancy of the hash table while still allowing for average case constant time operations, although it can degrade to O(n) in the worst case if the table becomes densely filled .

The enumerate() function in Python allows for easy iteration over a list by providing both the index and the value of each element during looping. This is beneficial when you need to track the position of elements as well as their content, which simplifies the code and makes it more readable, particularly when modifying a list or using conditional operations based on the index .

Inorder traversal visits the nodes of a binary search tree in sorted order: left child, root, then right child (Left → Root → Right). This contrasts with pre-order traversal, which visits nodes in the order of root, left, right (Root → Left → Right), and post-order traversal, which visits nodes left, right, root (Left → Right → Root). Inorder is particularly useful for retrieving elements in ascending order, whereas pre-order and post-order serve different hierarchical purposes .

Using a LIFO structure such as a stack in an undo/redo system ensures that the most recent actions are reversed first, emulating a perfect real-life undo functionality. This structure allows for efficient management of operations, where the latest action is the first to be undone or redone. However, it requires careful handling to clear the redo stack after new changes, which prevents invalid operations as real-time user actions replace any previously undone steps .

A doubly linked list would be advantageous over an array when dynamic memory allocation and frequent insertions or deletions are required. Unlike arrays, which have a fixed size and require shifting elements for insertions or deletions, a doubly linked list allows for efficient memory usage and easy manipulation of data nodes due to its bidirectional links .

Binary search requires the input list to be sorted in order to perform efficient comparisons and systematically eliminate half of the search space with each decision. If the list is not sorted, binary search would yield incorrect results, as the assumptions made about the relative order between elements would be invalid, rendering the algorithm ineffective .

Undo and redo operations in a stack-based system are both efficient, typically performed in constant time O(1), as they involve simply popping from one stack and pushing to another. The performance of these operations is largely independent of the number of actions tracked, provided the stacks are not excessively cleared or searched, which can degrade efficiency. However, factors such as memory overhead and the management of stack sizes can influence the system's overall responsiveness, particularly if excessive undo operations lead to large archival storage requirements .

You might also like