Updated 16 days ago | GitHub

Reverse Pairs

DSA for TIP Unit 1 Problem Set 2 (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 15 mins
  • 🛠️ Topics: Array Manipulation, Reordering

1: U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • Established a set (2-3) of test cases to verify their own solution later.
  • Established a set (1-2) of edge cases to verify their solution handles complexities.
  • Have fully understood the problem and have no clarifying questions.
  • Have you verified any Time/Space Constraints for this problem?
  • What is the goal of the problem?
    • The goal is to reorder a list of 2n elements arranged as consecutive pairs [x1, y1, x2, y2, ..., xn, yn] by swapping the elements of each pair, producing [y1, x1, y2, x2, ..., yn, xn].
  • What are the constraints on input?
    • The input will always contain an even number of elements.

HAPPY CASE
Input:
pairs = [1, 2, 3, 4, 5, 6]
Output:
[2, 1, 4, 3, 6, 5]
Explanation:
The pairs are reversed as [2, 1], [4, 3], [6, 5].

EDGE CASE
Input:
pairs = []
Output:
[]
Explanation:
An empty list results in an empty output.

2: M-atch

Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.

For array reordering problems, we want to consider the following approaches:

  • Iterative Traversal: Step through the list two elements at a time and construct the result by appending each pair in reversed order.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea:
Walk the list two elements at a time. For each consecutive pair, append the second element followed by the first element into a new list.

Steps:

  1. Initialize an empty list result.
  2. Iterate through indices i starting at 0 and stepping by 2:
    • Append the element at index i + 1 (the second element of the pair) to result.
    • Append the element at index i (the first element of the pair) to result.
  3. Return the result.

4: I-mplement

Implement the code to solve the algorithm.

def reverse_pairs(pairs):
    result = []
    for i in range(0, len(pairs) - 1, 2):  # Step through the list one pair (two elements) at a time
        result.append(pairs[i + 1])  # Add the second element of the pair
        result.append(pairs[i])      # Add the first element of the pair
    return result  # Return the reordered list with reversed pairs

5: R-eview

Review the code by running specific example(s) and recording values (watchlist) of your code’s variables along the way.

Example 1:

  • Input: pairs = [1, 2, 3, 4, 5, 6]
  • Expected Output: [2, 1, 4, 3, 6, 5]
  • Observed Output: [2, 1, 4, 3, 6, 5]

Example 2:

  • Input: pairs = [‘Batman’, ‘Robin’, ‘The Joker’, ‘Harley Quinn’]
  • Expected Output: [‘Robin’, ‘Batman’, ‘Harley Quinn’, ‘The Joker’]
  • Observed Output: [‘Robin’, ‘Batman’, ‘Harley Quinn’, ‘The Joker’]

6: E-valuate

Evaluate the performance of your algorithm and state any strong/weak or future potential work.

Assume n is the number of pairs in the input list (half its length).

  • Time Complexity: O(n) because we visit each pair exactly once.
  • Space Complexity: O(n) because we construct a new result list.