What is a data structure?
A data structure is a way to organize and store data for efficient access and modification, like arrays, linked lists, or trees.
What is an algorithm?
An algorithm is a step-by-step procedure to solve a problem, like sorting an array or searching for an item.
What is the difference between an array and a linked list?
An array stores elements in contiguous memory with fast access (O(1)) but fixed size; a linked list uses nodes with pointers, allowing dynamic size but slower access (O(n)).
What is a stack, and how does it work?
A stack is a LIFO (Last In, First Out) data structure. Elements are added (push) and removed (pop) from the top. Example: push(1), push(2), pop() → 2.
What is a queue, and how does it differ from a stack?
A queue is a FIFO (First In, First Out) data structure where elements are added at the rear and removed from the front.
What is time complexity in algorithms?
Time complexity measures how an algorithm’s runtime grows with input size, expressed in Big O notation (e.g., O(n) for linear time).
What does O(1) time complexity mean?
O(1) means constant time, where the algorithm’s runtime doesn’t depend on input size.
What is a binary search, and when can it be used?
Binary search finds an element in a sorted array by halving the search space each step (O(log n)). It requires a sorted input.
What is a binary tree?
A binary tree is a hierarchical structure where each node has at most two children (left and right).
What is the difference between BFS and DFS?
Breadth-First Search (BFS) explores nodes level by level using a queue; Depth-First Search (DFS) explores as far as possible along a branch using a stack or recursion.
What is a hash table, and how does it work?
A hash table stores key-value pairs using a hash function to compute an index for fast lookup (average O(1)).
What is a linked list, and how do you reverse one?
A linked list is a chain of nodes, each with data and a next pointer. To reverse, iterate and update pointers.
What is recursion, and when should you use it?
Recursion is when a function calls itself to solve smaller subproblems. Use it for problems like tree traversal.
What is a heap, and what are its types?
A heap is a tree-based structure where the parent is greater (max-heap) or smaller (min-heap) than its children.
What is the difference between a graph and a tree?
A graph is a collection of nodes and edges, possibly with cycles; a tree is a graph with no cycles and a single root.
What is bubble sort, and what is its time complexity?
Bubble sort repeatedly swaps adjacent elements if out of order. Time complexity is O(n²).
What is quicksort, and how does it work?
Quicksort picks a pivot, partitions the array around it, and recursively sorts subarrays. Average time complexity is O(n log n).
What is a trie, and what is it used for?
A trie is a tree where each node represents a character in a string, used for efficient string searches.
What is dynamic programming, and when is it used?
Dynamic programming solves problems by breaking them into overlapping subproblems, storing results to avoid recomputation.
What is a graph cycle, and how do you detect it?
A cycle is a path in a graph that returns to the starting node. Detect using DFS.
What is the Sliding Window pattern?
Sliding Window optimizes problems with subarrays/substrings by maintaining a dynamic window. Example: Find max sum of k consecutive elements in an array (O(n)).
How does the Two Pointers pattern work?
Two Pointers uses two indices to traverse an array or list, often from opposite ends or same direction. Example: Find two numbers summing to a target in a sorted array (O(n)).
What is the Fast and Slow Pointers pattern?
Fast and Slow Pointers uses two pointers moving at different speeds, often in linked lists. Example: Detect a cycle in a linked list (Floyd’s algorithm, O(n)).
What is the Binary Search pattern?
Binary Search halves the search space each step in a sorted dataset. Example: Find an element in a sorted array (O(log n)).