0% found this document useful (0 votes)
8 views7 pages

DSA Questions With Code Python

The document contains various Python data structure and algorithm (DSA) questions along with their code implementations. It covers topics such as arrays, strings, stacks, queues, linked lists, binary trees, graphs, and dynamic programming. Each section provides a specific problem and a corresponding function to solve it.

Uploaded by

Bhavya raval
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)
8 views7 pages

DSA Questions With Code Python

The document contains various Python data structure and algorithm (DSA) questions along with their code implementations. It covers topics such as arrays, strings, stacks, queues, linked lists, binary trees, graphs, and dynamic programming. Each section provides a specific problem and a corresponding function to solve it.

Uploaded by

Bhavya raval
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
You are on page 1/ 7

Python DSA Questions with Code

Arrays / Lists
Find Missing Number
def find_missing(arr): n = len(arr) + 1 expected_sum = n * (n + 1) // 2 return expected_sum - sum(arr)

Move Zeros to End


def move_zeros(arr): pos = 0 for i in range(len(arr)): if arr[i] != 0: arr[i], arr[pos] = arr[pos], arr[i] pos += 1

Kadane’s Algorithm
def max_subarray_sum(arr): max_sum = cur = arr[0] for x in arr[1:]: cur = max(x, cur + x) max_sum =
max(max_sum, cur) return max_sum

Second Largest Element


def second_largest(arr): first = second = float('-inf') for x in arr: if x > first: second, first = first, x elif x >
second and x != first: second = x return second
Strings
Check Palindrome
def is_palindrome(s): return s == s[::-1]

Anagram Check
from collections import Counter def are_anagrams(a, b): return Counter(a) == Counter(b)

First Non-Repeating Character


from collections import Counter def first_unique_char(s): count = Counter(s) for i, c in enumerate(s): if
count[c] == 1: return i return -1
Stacks and Queues
Valid Parentheses
def is_valid(expr): stack = [] pair = {')': '(', ']': '[', '}': '{'} for char in expr: if char in pair.values():
stack.append(char) elif char in pair: if not stack or stack.pop() != pair[char]: return False return not stack
Linked List
Reverse a Linked List
class Node: def __init__(self, val): self.val = val self.next = None def reverse(head): prev = None while
head: nxt = head.next head.next = prev prev = head head = nxt return prev
Binary Trees
Inorder Traversal
def inorder(root): if root: inorder(root.left) print(root.val) inorder(root.right)
Graphs
BFS Traversal
from collections import deque def bfs(graph, start): visited = set() queue = deque([start]) while queue: node
= queue.popleft() if node not in visited: visited.add(node) queue.extend(graph[node])
Dynamic Programming
0/1 Knapsack
def knapsack(wt, val, W): n = len(wt) dp = [[0]*(W+1) for _ in range(n+1)] for i in range(1, n+1): for w in
range(W+1): if wt[i-1] <= w: dp[i][w] = max(dp[i-1][w], val[i-1] + dp[i-1][w-wt[i-1]]) else: dp[i][w] = dp[i-1][w]
return dp[n][W]

You might also like