Skip to content

Python array DSA questions with increasing complexity #1

@Pankaj-Str

Description

@Pankaj-Str

Question 1: Array Element Replacement

# Given an array of integers, write a function that replaces each element 
# with the sum of its neighboring elements. For edge elements, consider only
# the existing neighbor.
#
# Example:
# Input: [4, 2, 5, 7, 1]
# Output: [2, 9, 9, 6, 7]
#
# Explanation: 
# - 4 becomes 2 (only right neighbor)
# - 2 becomes 9 (4 + 5)
# - 5 becomes 9 (2 + 7)
# - 7 becomes 6 (5 + 1)
# - 1 becomes 7 (only left neighbor)

def replace_with_neighbors(arr):
    # Your code here
    pass

Question 2: Alternate Sum and Product

# Write a function that returns a new array where elements at even indices
# are the sum of all previous elements, and elements at odd indices are the
# product of all previous elements. First element should be 0.
#
# Example:
# Input: [2, 3, 4, 5]
# Output: [0, 2, 5, 30]
#
# Explanation:
# - Index 0: 0 (no previous elements)
# - Index 1: 2 (product of [2])
# - Index 2: 5 (sum of [2, 3])
# - Index 3: 30 (product of [2, 3, 4])

def alternate_sum_product(arr):
    # Your code here
    pass

Question 3: Circular Array Rotation

# Given an array and a number k, write a function that rotates the array
# k steps to the right, treating the array as circular. Negative k means
# rotation to the left.
#
# Example:
# Input: arr = [1, 2, 3, 4, 5], k = 2
# Output: [4, 5, 1, 2, 3]
#
# Input: arr = [1, 2, 3, 4, 5], k = -1
# Output: [2, 3, 4, 5, 1]

def rotate_array(arr, k):
    # Your code here
    pass

Question 4: Peak and Valley Sort

# Write a function that rearranges an array in peak-valley pattern:
# nums[0] >= nums[1] <= nums[2] >= nums[3] <= nums[4]...
# You may assume the input array has no duplicate elements.
#
# Example:
# Input: [5, 3, 1, 2, 4]
# Output: [5, 1, 4, 2, 3]
#
# Explanation: 
# 5 >= 1 <= 4 >= 2 <= 3 (pattern satisfied)

def peak_valley_sort(arr):
    # Your code here
    pass

Question 5: Window Maximum

# Write a function that finds the maximum value in each window of size k
# as it slides through the array from left to right.
#
# Example:
# Input: arr = [1, 3, -1, -3, 5, 3, 6, 7], k = 3
# Output: [3, 3, 5, 5, 6, 7]
#
# Explanation:
# Window 1: [1, 3, -1] => max = 3
# Window 2: [3, -1, -3] => max = 3
# Window 3: [-1, -3, 5] => max = 5
# Window 4: [-3, 5, 3] => max = 5
# Window 5: [5, 3, 6] => max = 6
# Window 6: [3, 6, 7] => max = 7

def sliding_window_max(arr, k):
    # Your code here
    pass

Each question tests different array manipulation concepts:

  1. Question 1: Array traversal and neighbor access
  2. Question 2: Running sum/product and index manipulation
  3. Question 3: Array rotation and modulo arithmetic
  4. Question 4: Array rearrangement and comparison
  5. Question 5: Sliding window technique

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions