Data Structure Algorithms
DATA STRUCTURES & ALGORITHMS
Introduction to DSA & Arrays
What is DSA?
Data Structures and Algorithms (DSA) help you organize data and solve problems efficiently.
- Data Structure: How data is stored (Arrays, Linked List, Trees)
- Algorithm: Step-by-step solution to a problem
Why Learn DSA?
- Improves problem-solving skills
- Helps write faster code
- Important for interviews
- Used in real-world apps
Time & Space Complexity
Time Complexity
| Complexity | Example | Meaning |
|---|---|---|
| O(1) | Access element | Constant |
| O(n) | Loop | Linear |
| O(n²) | Nested loops | Quadratic |
| O(log n) | Binary Search | Logarithmic |
Space Complexity
arr = [1,2,3,4] # O(n)
Arrays in Python
1D Array
arr = [10, 20, 30, 40]
2D Array
matrix = [
[1,2],
[3,4]
]
Operations
Traversalfor i in arr:
print(i)
Insertion
arr.append(40)
Deletion
arr.remove(20)
Search
if 30 in arr:
print("Found")
Strings in Python
What is String?
name = "Prem"
message = "Hello"
Strings are immutable.
Indexing
text = "Python"
print(text[0])
print(text[-1])
Slicing
print(text[0:3])
print(text[::-1])
String Methods
split()"I love Python".split()
join()
" ".join(['I','love','Python'])
replace()
"I love Java".replace("Java","Python")
Logic Problems
Palindrome
text = "madam"
print(text == text[::-1])
Anagram
print(sorted("listen") == sorted("silent"))
Practice Problems
1. Count Vowels
text = input().lower()
vowels = "aeiou"
v = c = 0
for ch in text:
if ch.isalpha():
if ch in vowels:
v+=1
else:
c+=1
print(v, c)
2. Longest Word
sentence = input()
print(max(sentence.split(), key=len))
3. Reverse Words
sentence = input()
print(" ".join(sentence.split()[::-1]))
🚀 Practice Daily • Crack Interviews • Master DSA
Comments
Post a Comment