Docsity

Time Complexity of Algorithms and Data Structures, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
Time Complexity of
Algorithms and Data
Structures
Array - ANSOperations and Time Complexity:Access: O(1)Search:
O(n)Insertion: O(n)Deletion: O(n)
Example Interview Question: "Given an array of integers, find the sum of
elements at even indices."
Binary Search - ANSTime Complexity: O(log n)
Example Interview Question: "Given a sorted array and a key, implement a
function that finds the position of the key in the array using binary search."
Binary Search Tree (BST) - ANSOperations and Time Complexity:Insert: O(log
n) average, O(n) worstDelete: O(log n) average, O(n) worstSearch: O(log n)
average, O(n) worst
Example Interview Question: "Write a function to check if a binary tree is a
valid BST."
Breadth-First Search (BFS) - ANSTime Complexity: O(V + E) where V is the
number of vertices and E is the number of edges.
Example Interview Question: "How would you find the shortest path in an
unweighted graph using BFS?"
Bubble Sort - ANSTime Complexity: Average and Worst: O(n^2), Best: O(n) if
already sorted
Example Interview Question: "Why is bubble sort not efficient for large
datasets, and when might it be a good choice?"
pf3

Partial preview of the text

Download Time Complexity of Algorithms and Data Structures and more Exams Data Structures and Algorithms in PDF only on Docsity!

Time Complexity of

Algorithms and Data

Structures

Array - ANSOperations and Time Complexity:Access: O(1)Search: O(n)Insertion: O(n)Deletion: O(n) Example Interview Question: "Given an array of integers, find the sum of elements at even indices." Binary Search - ANSTime Complexity: O(log n) Example Interview Question: "Given a sorted array and a key, implement a function that finds the position of the key in the array using binary search." Binary Search Tree (BST) - ANSOperations and Time Complexity:Insert: O(log n) average, O(n) worstDelete: O(log n) average, O(n) worstSearch: O(log n) average, O(n) worst Example Interview Question: "Write a function to check if a binary tree is a valid BST." Breadth-First Search (BFS) - ANSTime Complexity: O(V + E) where V is the number of vertices and E is the number of edges. Example Interview Question: "How would you find the shortest path in an unweighted graph using BFS?" Bubble Sort - ANSTime Complexity: Average and Worst: O(n^2), Best: O(n) if already sorted Example Interview Question: "Why is bubble sort not efficient for large datasets, and when might it be a good choice?"

Depth-First Search (DFS) - ANSTime Complexity: O(V + E) where V is the number of vertices and E is the number of edges. Example Interview Question: "How would you detect a cycle in a directed graph using DFS?" Graph - ANSOperations and Time Complexity:Add Vertex: O(1)Add Edge: O(1)Search: O(V + E) for BFS and DFS Example Interview Question: "Implement a graph traversal algorithm to find the shortest path between two nodes." Hash Table - ANSOperations and Time Complexity:Insert: O(1) average, O(n) worstDelete: O(1) average, O(n) worstSearch: O(1) average, O(n) worst Example Interview Question: "How would you handle collisions in a hash table? Heap - ANSOperations and Time Complexity:Insert: O(log n)Delete Max/Min: O(log n)Find Max/Min: O(1) Example Interview Question: "How would you find the kth largest element in a min-heap?" Insertion Sort - ANSTime Complexity: Average and Worst: O(n^2), Best: O(n) if nearly sorted Example Interview Question: "How does insertion sort behave on nearly sorted data?" Linked List - ANSOperations and Time Complexity:Access: O(n)Search: O(n)Insertion: O(1)Deletion: O(1) Example Interview Question: "How would you reverse a singly linked list?"