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

ComplexityExampleMeaning
O(1)Access elementConstant
O(n)LoopLinear
O(n²)Nested loopsQuadratic
O(log n)Binary SearchLogarithmic

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

Traversal
for 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

Popular posts from this blog

PROBLEM SOLVING USING C

JavaScript