Sorting, Graph, Dynamic programming, Searching, Recursion,

0.0(0)
studied byStudied by 0 people
full-widthCall with Kai
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/36

flashcard set

Earn XP

Description and Tags

Questions which are related to the title

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

37 Terms

1
New cards

What is recursion?

  • A function that calls itself to solve a smaller version of the problem.

2
New cards

What are the two cases in a recursive function?

  1. Base case (terminates recursion)

  2. recursive case (function calls itself).

3
New cards

Difference between recursion and iteration?

  • Recursion uses function calls and stack memory;

  • iteration uses loops without extra stack overhead.

4
New cards

Give examples of problems solved with recursion

  1. Fibonacci

  2. Factorial

  3. Tree traversals

  4. Graph traversals (DFS/BFS)

  5. Towers of Hanoi."

5
New cards

What is sorting?

Sorting arranges elements of a list in a specific order (ascending or descending).

6
New cards

What is the best complexity possible for comparison-based sorting?

O(n log n).

7
New cards

Name two non-comparison sorting algorithms.

  1. Counting Sort

  2. Radix Sort.

8
New cards

What is a stable sort?

A sorting algorithm that preserves the relative order of equal elements

9
New cards

Which algorithms are recursive and which are not?

  • Recursive → QuickSort, MergeSort;

  • Non-recursive → Selection Sort, Insertion Sort."

10
New cards

What is searching in computer science?

The process of finding an item with specified properties in a collection.

11
New cards

Why is searching important?

To retrieve data efficiently from large datasets.

12
New cards

What are the main types of searching algorithms?,

  1. Linear Search

  2. Binary Search

  3. Interpolation Search

  4. Hash-based search."

13
New cards

What is the complexity of Linear Search?

O(n).

14
New cards

What is the complexity of Binary Search?

O(log n) (on sorted data).

15
New cards

What is a graph?

A pair (V, E) where V is a set of vertices and E is a set of edges."

16
New cards

What is the difference between directed and undirected graphs?

  • Directed graphs have edges with a direction (u→v)

  • Undirected graphs have bidirectional edges (u,v)."

17
New cards

What is a spanning tree?

A subgraph that connects all vertices with minimum edges and no cycles.

18
New cards

Which algorithm finds the shortest path in a graph with positive weights?

Dijkstra’s Algorithm.

19
New cards

"Which algorithm finds all-pairs shortest path, even with negative weights?

Floyd–Warshall Algorithm.

20
New cards

What is Dynamic Programming (DP)?

A method that solves problems by breaking them into subproblems, storing results to avoid recomputation.

21
New cards

What are the two key properties of DP problems?

  1. Optimal substructure

  2. Overlapping subproblems.

22
New cards

What are the two approaches of DP?

  • Bottom-up (tabulation)

  • Top-down (memoization).

23
New cards

Give classic examples of DP problems.

  • Longest Common Subsequence

  • Knapsack

  • Matrix Chain Multiplication

  • Fibonacci

  • Bellman-Ford

  • TSP

24
New cards

How is DP different from Divide & Conquer?

  • Divide & Conquer solves independent subproblems,

  • DP handles overlapping subproblems using memoization.

25
New cards

What is the average and worst case of QuickSort?

  • Average = O(n log n)

  • Worst = O(n²). Trick → avoid worst case by using random pivot."

26
New cards

What is the time complexity of MergeSort?

  • O(n log n).

  • Recurrence → T(n) = 2T(n/2) + O(n).

27
New cards

Trick to quickly compare sorting algorithms?

  1. Insertion → small n, nearly sorted;

  2. MergeSort → guaranteed O(n log n);

  3. QuickSort → fastest in practice but avoid bad pivots;

  4. HeapSort → good for memory-constrained."

28
New cards

How does Binary Search work?

Repeatedly divide sorted array into halves until element is found or interval is empty.

29
New cards

What is the recurrence relation for Binary Search?

T(n) = T(n/2) + O(1) → O(log n)

30
New cards

Trick for choosing searching algorithm?

  1. If array is sorted & static → Binary Search;

  2. if dynamic (many insert/delete) → Hashing or Trees.

31
New cards

What is the recurrence for Fibonacci using recursion?

T(n) = T(n-1) + T(n-2) + O(1) → O(2^n).

32
New cards

How to optimize recursive Fibonacci?

Use Dynamic Programming (memoization) → reduces to O(n).

33
New cards

What is the time complexity of BFS and DFS?

O(V + E).

34
New cards

Trick to remember BFS vs DFS?

  • BFS = Queue (level order)

  • DFS = Stack (depth-first).

35
New cards

Which algorithm finds the Minimum Spanning Tree (MST)?

  1. Kruskal (greedy with edges)

  2. Prim (greedy with vertices)

  3. Trick: Prim = Grow a tree

  4. Kruskal = Join forests.

36
New cards

Formula for Longest Common Subsequence (LCS)?

if X[i] = Y[j];
    LCS[i][j] = LCS[i-1][j-1] + 1 ;
else max(LCS[i-1][j], LCS[i][j-1]);

37
New cards

Trick for recognizing DP problems?

Look for overlapping subproblems and optimal substructure.

Explore top flashcards