← Mastercard Interview Insights

Mastercard·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Interviewed for a SWE role at Mastercard and got a classic arrays problem that sounds easy until you actually think about the comparison count.

Questions Asked (1)

Q1

Given an array of integers, find both the minimum and maximum values while minimizing the total number of comparisons made.

Algorithms & Data Structures
Author's notes

My first instinct was two separate linear scans, which works but doubles the comparisons.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem constraints (e.g., array size, whether it can be empty, data types) and then present the optimal pairwise comparison algorithm that finds both min and max in about 3n/2 comparisons. Explain the algorithm step-by-step, analyze its time complexity, and compare it to the naive approach to highlight the efficiency gain.

Pro tip: Mention that for small arrays (n <= 2), the naive approach is actually optimal, and for general n, the pairwise method reduces comparisons by ~25% compared to the naive 2n-2 approach. This shows you understand edge cases and practical trade-offs.

1. Clarify requirements and constraints

Ask about array size, whether it can be empty, if elements are distinct, and if there are any memory constraints. This ensures you understand the problem fully before diving into the solution.

2. Discuss naive approach and its comparison count

Explain that initializing min and max to the first element and then comparing each subsequent element to both min and max takes 2(n-1) comparisons in the worst case. This sets a baseline for optimization.

3. Present the optimal pairwise comparison algorithm

Describe how to process elements in pairs: compare the two elements of each pair to each other (1 comparison), then compare the smaller to the current min and the larger to the current max (2 comparisons per pair). Handle the case of an odd number of elements by comparing the last element to both min and max (2 comparisons).

4. Analyze comparison count and time complexity

Show that for n even, total comparisons = 3n/2 - 2; for n odd, total comparisons = 3(n-1)/2 + 2 = (3n-1)/2. Emphasize that this is optimal and the time complexity remains O(n).

5. Discuss edge cases and potential optimizations

Mention handling of empty array (return null or throw exception), single element (min=max=element), and two elements (1 comparison). Also note that for small n, the naive approach may be simpler and equally efficient.

Key Points to Mention

  • The optimal number of comparisons is ⌈3n/2⌉ - 2, which is the theoretical lower bound for finding both min and max.
  • The pairwise comparison method reduces comparisons by about 25% compared to the naive 2n-2 approach.
  • Time complexity remains O(n), but the constant factor is improved.
  • Edge cases: empty array, single element, two elements, and odd/even array lengths.
  • Space complexity is O(1) as only a few variables are needed.
  • The algorithm can be implemented iteratively or recursively, but iterative is more space-efficient.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.