DSA Flashcards

(40 cards)

1
Q

What is a data structure?

A

A data structure is a way to organize and store data for efficient access and modification, like arrays, linked lists, or trees.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is an algorithm?

A

An algorithm is a step-by-step procedure to solve a problem, like sorting an array or searching for an item.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference between an array and a linked list?

A

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)).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a stack, and how does it work?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a queue, and how does it differ from a stack?

A

A queue is a FIFO (First In, First Out) data structure where elements are added at the rear and removed from the front.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is time complexity in algorithms?

A

Time complexity measures how an algorithm’s runtime grows with input size, expressed in Big O notation (e.g., O(n) for linear time).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does O(1) time complexity mean?

A

O(1) means constant time, where the algorithm’s runtime doesn’t depend on input size.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a binary search, and when can it be used?

A

Binary search finds an element in a sorted array by halving the search space each step (O(log n)). It requires a sorted input.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a binary tree?

A

A binary tree is a hierarchical structure where each node has at most two children (left and right).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the difference between BFS and DFS?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a hash table, and how does it work?

A

A hash table stores key-value pairs using a hash function to compute an index for fast lookup (average O(1)).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a linked list, and how do you reverse one?

A

A linked list is a chain of nodes, each with data and a next pointer. To reverse, iterate and update pointers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is recursion, and when should you use it?

A

Recursion is when a function calls itself to solve smaller subproblems. Use it for problems like tree traversal.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a heap, and what are its types?

A

A heap is a tree-based structure where the parent is greater (max-heap) or smaller (min-heap) than its children.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the difference between a graph and a tree?

A

A graph is a collection of nodes and edges, possibly with cycles; a tree is a graph with no cycles and a single root.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is bubble sort, and what is its time complexity?

A

Bubble sort repeatedly swaps adjacent elements if out of order. Time complexity is O(n²).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is quicksort, and how does it work?

A

Quicksort picks a pivot, partitions the array around it, and recursively sorts subarrays. Average time complexity is O(n log n).

18
Q

What is a trie, and what is it used for?

A

A trie is a tree where each node represents a character in a string, used for efficient string searches.

19
Q

What is dynamic programming, and when is it used?

A

Dynamic programming solves problems by breaking them into overlapping subproblems, storing results to avoid recomputation.

20
Q

What is a graph cycle, and how do you detect it?

A

A cycle is a path in a graph that returns to the starting node. Detect using DFS.

21
Q

What is the Sliding Window pattern?

A

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)).

22
Q

How does the Two Pointers pattern work?

A

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)).

23
Q

What is the Fast and Slow Pointers pattern?

A

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)).

24
Q

What is the Binary Search pattern?

A

Binary Search halves the search space each step in a sorted dataset. Example: Find an element in a sorted array (O(log n)).

25
What is the Depth-First Search (DFS) pattern?
DFS explores as far as possible along each branch in a graph or tree before backtracking. Example: Find all paths in a tree (O(V + E)).
26
What is the Breadth-First Search (BFS) pattern?
BFS explores nodes level by level using a queue. Example: Find shortest path in an unweighted graph (O(V + E)).
27
What is the Two Heaps pattern?
Two Heaps uses two priority queues (min/max) to solve problems like median finding. Example: Find median in a data stream (O(log n) per insert).
28
What is the Backtracking pattern?
Backtracking explores all possible solutions, pruning invalid paths. Example: Solve N-Queens by trying all placements (O(n!)).
29
What is the Dynamic Programming (DP) pattern?
DP breaks problems into overlapping subproblems, storing results to avoid recomputation. Example: Fibonacci with memoization (O(n)).
30
What is the Topological Sort pattern?
Topological Sort orders nodes in a directed acyclic graph (DAG) so that dependencies come first. Example: Course scheduling (O(V + E)).
31
What is the Greedy pattern?
Greedy makes locally optimal choices to achieve a global solution. Example: Minimum coins for change (may not always work, O(n)).
32
What is the Merge Intervals pattern?
Merge Intervals combines overlapping intervals in a sorted list. Example: Merge time intervals like [[1,3], [2,4]] → [[1,4]] (O(n log n)).
33
What is the Subsets pattern?
Subsets generates all possible combinations of a set, often using backtracking. Example: Find all subsets of [1,2] → [[], [1], [2], [1,2]] (O(2ⁿ)).
34
What is the Monotonic Stack pattern?
Monotonic Stack maintains elements in increasing/decreasing order to solve problems like next greater element. Example: Find next larger number in array (O(n)).
35
What is the Prefix Sum pattern?
Prefix Sum precomputes cumulative sums for efficient range queries. Example: Calculate sum of array range [i,j] in O(1) after O(n) preprocessing.
36
What is the Kadane’s Algorithm pattern?
Kadane’s finds the maximum subarray sum by tracking local and global maxima. Example: Max sum subarray of [-2,1,-3,4,-1] → 4 (O(n)).
37
What is the Divide and Conquer pattern?
Divide and Conquer splits a problem into smaller subproblems, solves them, and combines results. Example: Mergesort (O(n log n)).
38
What is the K-way Merge pattern?
K-way Merge combines k sorted lists using a min-heap or merging pairs. Example: Merge k sorted arrays (O(n log k), n total elements).
39
What is the Trie pattern?
Trie stores strings as a tree for efficient prefix-based searches. Example: Implement autocomplete (O(m) lookup, m is string length).
40
What is the Bit Manipulation pattern?
Bit Manipulation uses bitwise operations to solve problems efficiently. Example: Check if a number is a power of 2 (x & (x-1) == 0, O(1)).