Time Complexity of Algorithms and Data Structures
Array
Operations 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."
Linked List
Operations 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?"
Graph
Operations 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."
Queue
Operations and Time Complexity:Enqueue: O(1)Dequeue: O(1)Peek: O(1)IsEmpty: O(1) Example Interview Question: "How can you implement a queue using two stacks?"
Hash Table
Operations 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?
Binary Search Tree (BST)
Operations 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."
Heap
Operations 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?"
Stack
Operations and Time Complexity:Push: O(1)Pop: O(1)Peek: O(1)IsEmpty: O(1) Example Interview Question: "Implement a function that sorts a stack using only another stack as an auxiliary data structure."
Bubble Sort
Time 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?"
Insertion Sort
Time 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?"
Quick Sort
Time Complexity: Average: O(n log n), Worst: O(n^2) Example Interview Question: "Can you optimize quicksort for datasets where many elements are the same?"
Depth-First Search (DFS)
Time 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?"
Breadth-First Search (BFS)
Time 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?"
Binary Search
Time 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."
Merge Sort
Time Complexity: O(n log n) Example Interview Question: "How would you sort a large dataset with limited memory using merge sort?"
