0% found this document useful (0 votes)
391 views4 pages

Python DSA Coding Questions Solutions

The document contains a collection of Python solutions for various data structures and algorithms (DSA) coding questions. Each function addresses a specific problem, such as counting even and odd numbers, reversing a string, checking for prime numbers, and implementing sorting algorithms. The document serves as a reference for coding practices and problem-solving techniques in Python.

Uploaded by

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

Python DSA Coding Questions Solutions

The document contains a collection of Python solutions for various data structures and algorithms (DSA) coding questions. Each function addresses a specific problem, such as counting even and odd numbers, reversing a string, checking for prime numbers, and implementing sorting algorithms. The document serves as a reference for coding practices and problem-solving techniques in Python.

Uploaded by

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

DSA Coding Questions with Python Solutions

1. Count Even and Odd in List


def count_even_odd(arr):
even = odd = 0
for num in arr:
if num % 2 == 0:
even += 1
else:
odd += 1
return even, odd

2. Reverse a String
def reverse_string(s):
return s[::-1]

3. Prime Check
def is_prime(n):
if n <= 1: return False
for i in range(2, int(n**0.5)+1):
if n % i == 0:
return False
return True

4. Fibonacci Sequence
def fibonacci(n):
seq = []
a, b = 0, 1
for _ in range(n):
[Link](a)
a, b = b, a + b
return seq

5. Second Largest in Array


def second_largest(arr):
unique = list(set(arr))
[Link]()
return unique[-2] if len(unique) >= 2 else None

6. Palindrome Check
def is_palindrome(s):
return s == s[::-1]

7. 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]
return arr
8. Sum of Digits
def sum_digits(n):
return sum(int(d) for d in str(n))

9. Factorial
def factorial(n):
result = 1
for i in range(2, n+1):
result *= i
return result

10. Merge Two Sorted Arrays


def merge_sorted(a, b):
return sorted(a + b)

11. Frequency Count


def frequency(arr):
freq = {}
for x in arr:
freq[x] = [Link](x, 0) + 1
return freq

12. Missing Number (1 to N)


def missing_number(arr, n):
return n*(n+1)//2 - sum(arr)

13. Anagram Check


def are_anagrams(s1, s2):
return sorted(s1) == sorted(s2)

14. Remove Duplicates


def remove_duplicates(arr):
return list(set(arr))

15. Kadane's Algorithm


def max_subarray_sum(arr):
max_sum = curr = arr[0]
for num in arr[1:]:
curr = max(num, curr + num)
max_sum = max(max_sum, curr)
return max_sum

16. Armstrong Number


def is_armstrong(n):
digits = list(map(int, str(n)))
return sum(d**len(digits) for d in digits) == n

17. Rotate Array Left by K


def rotate_left(arr, k):
k = k % len(arr)
return arr[k:] + arr[:k]
18. Intersection of Arrays
def intersection(a, b):
return list(set(a) & set(b))

19. Move Zeroes to End


def move_zeroes(arr):
non_zero = [x for x in arr if x != 0]
return non_zero + [0]*(len(arr) - len(non_zero))

20. Binary Search


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

21. Perfect Square


import math
def is_perfect_square(n):
return [Link](n) ** 2 == n

22. Count Vowels and Consonants


def count_vowels_consonants(s):
vowels = 'aeiouAEIOU'
v = sum(1 for c in s if c in vowels)
c = sum(1 for c in s if [Link]() and c not in vowels)
return v, c

23. GCD of Two Numbers


import math
def gcd(a, b):
return [Link](a, b)

24. Binary to Decimal


def binary_to_decimal(bin_str):
return int(bin_str, 2)

25. First Non-Repeating Character


def first_non_repeat(s):
from collections import Counter
freq = Counter(s)
for ch in s:
if freq[ch] == 1:
return ch
return None

26. Stack using Array


class Stack:
def __init__(self): [Link] = []
def push(self, val): [Link](val)
def pop(self): return [Link]() if [Link] else None

27. Queue using Two Stacks


class QueueWithStacks:
def __init__(self): self.s1, self.s2 = [], []
def enqueue(self, x): [Link](x)
def dequeue(self):
if not self.s2:
while self.s1:
[Link]([Link]())
return [Link]() if self.s2 else None

28. Balanced Brackets


def is_balanced(expr):
stack = []
match = {')':'(', ']':'[', '}':'{'}
for ch in expr:
if ch in '([{':
[Link](ch)
elif ch in ')]}':
if not stack or [Link]() != match[ch]:
return False
return not stack

You might also like