0% found this document useful (0 votes)
3 views3 pages

Python DS Concepts

The document provides a series of programming exercises related to arrays, hashing, two pointers, sliding window, and stacks. Each section includes a question followed by a code snippet that demonstrates the solution. The examples cover tasks such as summing elements, finding maximum values, reversing arrays, counting frequencies, and checking for balanced parentheses.

Uploaded by

kate001atieno
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)
3 views3 pages

Python DS Concepts

The document provides a series of programming exercises related to arrays, hashing, two pointers, sliding window, and stacks. Each section includes a question followed by a code snippet that demonstrates the solution. The examples cover tasks such as summing elements, finding maximum values, reversing arrays, counting frequencies, and checking for balanced parentheses.

Uploaded by

kate001atieno
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/ 3

1.

Arrays

Q1: Create an array and find the sum of all elements.

arr = [1, 2, 3, 4, 5]

print(sum(arr)) # Output: 15

Q2: Find the maximum element in an array.

arr = [3, 7, 1, 9, 2]

print(max(arr)) # Output: 9

Q3: Reverse an array.

arr = [1, 2, 3, 4]

print(arr[::-1]) # Output: [4, 3, 2, 1]

2. Hashing

Q1: Count the frequency of each element.

arr = [1, 2, 2, 3, 1]

freq = {}

for num in arr:

freq[num] = freq.get(num, 0) + 1

print(freq) # Output: {1: 2, 2: 2, 3: 1}

Q2: Check if two arrays have a common element.

a = [1, 2, 3]

b = [4, 5, 3]

print(bool(set(a) & set(b))) # Output: True

3. Two Pointers
Q1: Check if an array has a pair that sums to a target (sorted array).

arr = [1, 2, 3, 4, 6]

target = 7

left, right = 0, len(arr) - 1

found = False

while left < right:

s = arr[left] + arr[right]

if s == target:

found = True

break

elif s < target:

left += 1

else:

right -= 1

print(found) # Output: True

4. Sliding Window

Q1: Find the maximum sum of any subarray of size k.

arr = [2, 1, 5, 1, 3, 2]

k=3

max_sum = current_sum = sum(arr[:k])

for i in range(k, len(arr)):

current_sum += arr[i] - arr[i - k]

max_sum = max(max_sum, current_sum)

print(max_sum) # Output: 9

5. Stacks
Q1: Reverse a string using a stack.

s = "hello"

stack = list(s)

reversed_str = ""

while stack:

reversed_str += stack.pop()

print(reversed_str) # Output: "olleh"

Q2: Check for balanced parentheses.

def is_balanced(s):

stack = []

mapping = {')': '(', ']': '[', '}': '{'}

for char in s:

if char in mapping.values():

stack.append(char)

elif char in mapping:

if not stack or stack[-1] != mapping[char]:

return False

stack.pop()

return not stack

print(is_balanced("({[]})")) # Output: True

You might also like