Question Bank

JavaScript Largest Difference Without Sort: Two Approaches Quiz

Difficulty: Medium

Two seeded approaches to compute the largest pairwise difference in an array without sorting (single-pass min/max and the brute-force nested loop), plus two companions on prefix tracking and a negative-numbers twist.

Question Bank
/

JavaScript Largest Difference Without Sort: Two Approaches Quiz

JavaScript Largest Difference Without Sort: Two Approaches Quiz

Two seeded approaches to compute the largest pairwise difference in an array without sorting (single-pass min/max and the brute-force nested loop), plus two companions on prefix tracking and a negative-numbers twist.

Question Bank
Medium
JavaScript
4 questions
quiz
arrays
interview-prep
array-manipulation-patterns

184 views

1

Implement largestDiff(arr) using a SINGLE-PASS min/max tracker. Walk the array once, keep running min and max, then return max - min. Sorting is not allowed.

Examples

Example 1:

Input: [2, 8, 24, 7, 4, 13, 4, 6, 30, 8]
Output: 28
Explanation: max is 30 and min is 2, so the largest pairwise difference is 30 - 2 = 28.

Example 2:

Input: [5]
Output: 0
Explanation: With a single element, min equals max, so the difference is 0.