0% found this document useful (0 votes)
26 views9 pages

Python DSA Guide for Beginners to Advanced

Uploaded by

amitzend68
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views9 pages

Python DSA Guide for Beginners to Advanced

Uploaded by

amitzend68
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Data Structures and Algorithms (DSA) using

Python
Language level: Beginner to Advanced
Explanation style: Simple, clear, and exam/interview-friendly
Best for: Students, beginners, and Python learners

1. What is DSA?
Data Structure: A way to store and organize data so we can use it
efficiently.
Algorithm: Step-by-step instructions to solve a problem.

Why DSA is important?


 Makes programs faster □
 Saves memory □
 Helps in interviews □
 Needed for real-world applications

2. Time and Space Complexity


Time Complexity
How much time an algorithm takes.

Common notations: - O(1) – Constant time - O(n) – Linear time - O(n²)


– Quadratic time - O(log n) – Logarithmic time
Example:
for i in range(n):
print(i) #
O(n)

Space Complexity
How much memory an algorithm uses.

3. Python Basics for DSA


Variables
a = 10
name = "Python"

Data Types
 int
 float
 string
 bool

Conditional Statements
if a > b:
print("A is greater")
else:
print("B is greater")

Loops
for i in range(5):
print(i)

while i < 5:
i += 1
4. Arrays (List in Python)
An array stores multiple values in a single variable.
arr = [10, 20, 30, 40]

Operations
 Access: arr[0]
 Insert: [Link](50)
 Delete: [Link](20)

Time Complexity
 Access: O(1)
 Search: O(n)

5. Strings
A string is a sequence of characters.
s = "hello"

Common Operations
len(s)
[Link]()
[Link]()
s[::-1] # reverse

6. Searching Algorithms
Linear Search
Search element one by one.
def linear_search(arr, x):
for i in range(len(arr)):
if arr[i] == x:
return i
return -1

Time Complexity: O(n)

Binary Search (Sorted Array)


def binary_search(arr, x):
low, high = 0,
len(arr)-1 while low <=
high:
mid = (low + high)//2
if arr[mid] == x:
return mid
elif arr[mid] < x:
low = mid + 1
else:
high = mid - 1
return -1

Time Complexity: O(log n)

7. Sorting Algorithms
Bubble Sort
def
bubble_sort(arr
): n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]

Time Complexity: O(n²)

Selection Sort
def selection_sort(arr):
for i in
range(len(arr)):
min_idx = i
for j in range(i+1, len(arr)):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]

Insertion Sort
def insertion_sort(arr):
for i in range(1,
len(arr)): key = arr[i]
j = i - 1
while j >= 0 and key < arr[j]:
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key

Merge Sort (Divide & Conquer)


def merge_sort(arr):
if len(arr) > 1:
mid =
len(arr)//2 L =
arr[:mid]
R =
arr[mid:]
merge_sort(L
)
merge_sort(R
) i = j = k
= 0
while i < len(L) and j < len(R):
if L[i] < R[j]:
arr[k] =
L[i] i += 1
else:
arr[k] = R[j]
j += 1
k += 1

Time Complexity: O(n log n)

8. Stack
Follows LIFO (Last In First Out)
stack = []
[Link](10)
[Link]()

Applications: - Undo/Redo - Expression evaluation

9. Queue
Follows FIFO (First In First Out)
from collections import
deque queue = deque()
[Link](10)
[Link]()

Applications: - Scheduling - Breadth First Search

10. Linked List


Each node has data + next address

Types
 Singly Linked List
 Doubly Linked List
 Circular Linked List
Basic Node:
class Node:
def init (self,
data): [Link] =
data [Link] =
None
11. Recursion
Function calling itself.
def factorial(n):
if n == 0:
return 1
return n * factorial(n-1)

12. Trees
Binary Tree
Each node has at most 2 children.

Binary Search Tree (BST)


 Left < Root < Right

Applications: - Searching - Hierarchical data

13. Graphs
Graph = Nodes + Edges

Types
 Directed
 Undirected

Traversals
 BFS (Queue)
 DFS (Stack / Recursion)
14. Hashing
Stores data in key-value form.
d = {}
d["name"] = "Python"

Time Complexity: - Search: O(1)

15. Dynamic Programming (DP)


Break problem into smaller sub-problems.
Example: Fibonacci
def fib(n):
dp = [0, 1]
for i in range(2, n+1):
[Link](dp[i-1] + dp[i-
2])
return dp[n]

16. Greedy Algorithms


Choose best option at each step.

Example: - Coin Change - Activity Selection

17. Backtracking
Try all possibilities and go back if wrong. Example: -
N-Queens - Sudoku
18. Important Interview Topics
 Arrays & Strings
 Stack & Queue
 Recursion
 Sorting & Searching
 Trees & Graphs
 Dynamic Programming

19. Practice Platforms


 LeetCode
 HackerRank
 CodeChef
 GeeksforGeeks

20. Final Tip □


✔Understand concepts ✔ Practice daily ✔ Write code yourself ✔ Focus on
logic, not syntax

✨If you want: - PDF version - Only interview questions - More examples &
diagrams - Step-by-step problem solving
Just tell me □

You might also like