Data Structures Flashcards

(35 cards)

1
Q

What is the time complexity of accessing an element by index in an array?

A

O(1) - arrays provide constant-time random access because elements are stored in contiguous memory locations.

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

What is the time complexity of inserting an element at the beginning of an array?

A

O(n) - all existing elements must be shifted one position to the right.

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

What is the time complexity of inserting at the end of a dynamic array (like TypeScript’s array)?

A

O(1) amortized - occasionally O(n) when the array needs to resize, but averaged over many operations it’s constant.

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

What is a linked list and when would you use it over an array?

A

A linked list is a sequence of nodes where each node contains data and a pointer to the next node. Use it when you need frequent insertions/deletions at arbitrary positions and don’t need random access.

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

What is the time complexity of inserting at the beginning of a singly linked list?

A

O(1) - just create a new node and point it to the current head.

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

What is the time complexity of inserting at the end of a singly linked list without a tail pointer?

A

O(n) - you must traverse the entire list to find the last node.

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

What is the difference between a singly linked list and a doubly linked list?

A

A singly linked list has nodes with only a ‘next’ pointer. A doubly linked list has both ‘next’ and ‘prev’ pointers, allowing traversal in both directions and O(1) deletion if you have a reference to the node.

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

What is a stack and what is its access pattern?

A

A stack is a LIFO (Last In, First Out) data structure. The last element added is the first one removed. Think of a stack of plates.

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

What are the time complexities of push, pop, and peek operations on a stack?

A

All O(1) - these operations only affect the top of the stack.

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

What is a queue and what is its access pattern?

A

A queue is a FIFO (First In, First Out) data structure. The first element added is the first one removed. Think of a line at a store.

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

What are the time complexities of enqueue and dequeue operations on a queue?

A

O(1) for both when implemented with a linked list or circular array. With a simple array, dequeue can be O(n) due to shifting.

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

What is a deque (double-ended queue)?

A

A deque allows insertion and deletion at both ends in O(1) time. It combines the capabilities of both stacks and queues.

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

What is a hash table/hash map?

A

A data structure that maps keys to values using a hash function. The hash function converts keys into array indices for fast lookups.

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

What are the average and worst-case time complexities for hash table operations (insert, delete, lookup)?

A

Average: O(1) for all operations. Worst case: O(n) when many keys hash to the same index (hash collisions).

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

What is a hash collision and how is it typically handled?

A

A collision occurs when two keys hash to the same index. Common solutions: chaining (linked list at each index) or open addressing (probing for next empty slot).

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

What makes a good hash function?

A

It should be deterministic, distribute keys uniformly across the array, be fast to compute, and minimize collisions.

17
Q

What is the load factor in a hash table and why does it matter?

A

Load factor = number of entries / table size. Higher load factors increase collision probability. Tables typically resize when load factor exceeds 0.7-0.75.

18
Q

What is a Set and how does it differ from an array?

A

A Set stores unique values with O(1) average lookup/insert/delete. Arrays allow duplicates and have O(n) lookup unless sorted.

19
Q

What is a binary tree?

A

A hierarchical data structure where each node has at most two children, referred to as left and right children.

20
Q

What is a Binary Search Tree (BST)?

A

A binary tree where for each node, all values in its left subtree are less than the node’s value, and all values in its right subtree are greater.

21
Q

What are the time complexities for search, insert, and delete in a BST?

A

Average: O(log n). Worst case (unbalanced/skewed tree): O(n).

22
Q

What is a balanced binary tree and why does it matter?

A

A tree where the height difference between left and right subtrees of any node is at most 1. Balancing ensures O(log n) operations instead of O(n).

23
Q

Name two types of self-balancing BSTs.

A

AVL trees (strict balancing, faster lookups) and Red-Black trees (less strict, faster insertions/deletions). TreeMap in Java uses Red-Black.

24
Q

What is a heap?

A

A complete binary tree that satisfies the heap property: in a max-heap, each parent is greater than or equal to its children; in a min-heap, each parent is less than or equal to its children.

25
What are the time complexities for heap operations?
Insert: O(log n), Extract min/max: O(log n), Peek min/max: O(1), Heapify an array: O(n).
26
How is a heap typically implemented?
Using an array. For node at index i: left child at 2i+1, right child at 2i+2, parent at floor((i-1)/2).
27
What is a priority queue and how is it typically implemented?
An abstract data type where elements have priorities and higher priority elements are served first. Typically implemented using a heap.
28
What is a trie (prefix tree)?
A tree-like data structure for storing strings where each node represents a character. Common prefixes share the same path from root. Excellent for autocomplete and spell-checking.
29
What are the time complexities for trie operations?
Insert and search are both O(m) where m is the length of the string, regardless of how many strings are stored.
30
When would you use a trie over a hash set of strings?
When you need prefix matching, autocomplete, or to find all strings with a common prefix. Hash sets only support exact matching.
31
What is a graph?
A data structure consisting of vertices (nodes) and edges connecting them. Edges can be directed or undirected, weighted or unweighted.
32
What are the two common ways to represent a graph in code?
Adjacency matrix (2D array where matrix[i][j] indicates edge between i and j) and adjacency list (array/map where each vertex stores its neighbors).
33
When would you use an adjacency matrix vs an adjacency list?
Matrix: dense graphs, need O(1) edge lookup. List: sparse graphs, need to iterate over neighbors efficiently, less memory for sparse graphs.
34
What is the space complexity of an adjacency matrix vs adjacency list?
Matrix: O(V²). List: O(V + E). For sparse graphs (E << V²), adjacency list is much more space efficient.
35
What is a directed acyclic graph (DAG)?
A directed graph with no cycles. Used in dependency resolution, task scheduling, and represents prerequisites/dependencies.