0
0
DSA Pythonprogramming~10 mins

Two Pointer Technique on Arrays in DSA Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the left pointer at the start of the array.

DSA Python
arr = [1, 3, 5, 7, 9]
left = [1]
right = len(arr) - 1
print(left, right)
Drag options to blanks, or click blank then click option'
A1
Blen(arr)
C0
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Starting left pointer at 1 or -1 instead of 0.
2fill in blank
medium

Complete the code to move the right pointer one step to the left.

DSA Python
right = 5
right = right [1] 1
print(right)
Drag options to blanks, or click blank then click option'
A/
B-
C*
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of - to move left.
3fill in blank
hard

Fix the error in the loop condition to stop when left pointer crosses right pointer.

DSA Python
left = 0
right = 4
while left [1] right:
    print(left, right)
    left += 1
    right -= 1
Drag options to blanks, or click blank then click option'
A<
B>
C<=
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= or >= causing extra iterations.
4fill in blank
hard

Fill both blanks to create a two-pointer loop that swaps elements until pointers meet.

DSA Python
arr = [2, 4, 6, 8, 10]
left = 0
right = len(arr) - 1
while left [1] right:
    arr[left], arr[right] = arr[right], arr[left]
    left = left [2] 1
    right -= 1
print(arr)
Drag options to blanks, or click blank then click option'
A<
B+
C-
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= causes extra swaps.
Decrementing left pointer instead of incrementing.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each element to its index if the element is even.

DSA Python
arr = [1, 2, 3, 4, 5]
even_index_map = [1]: [2] for [3] in range(len(arr)) if arr[i] % 2 == 0
print(even_index_map)
Drag options to blanks, or click blank then click option'
Aarr[i]
Bi
Darr
Attempts:
3 left
💡 Hint
Common Mistakes
Using arr instead of arr[i] as key.
Using wrong loop variable.