Sort Adjacent Pairs
Updated
Problem statement
Given an integer array arr, split it into pairs from the left, arrange the two elements of each pair in ascending order, and return the resulting array.
The pairs are (arr[0], arr[1]), (arr[2], arr[3]), and so on. In other words, swap the two elements of a pair when the left one is greater than the right one.
If arr has an odd number of elements, leave the last element unchanged.
Note: You don't need the most optimal solution. Any solution with time complexity no worse than O(arr.length^2) fits within the time limit.
Function
sortAdjacentPairs(arr: int[]) → int[]Examples
Example 1
arr = [1,5,7,3,2,1]return = [1,5,3,7,1,2]- The given array has
3pairs:(1, 5),(7, 3),(2, 1). - After arranging the elements of each pair in ascending order, we get
(1, 5),(3, 7),(1, 2). - So, the answer is
[1, 5, 3, 7, 1, 2].
Example 2
arr = [6,7,8,8,5,3,2]return = [6,7,8,8,3,5,2]The pairs are (6, 7), (8, 8), (5, 3), and the last element 2 has no partner. Only (5, 3) is out of order, so it becomes (3, 5), and 2 stays in place.
Constraints
1 <= arr.length <= 1000-10^9 <= arr[i] <= 10^9
More Airbnb Online Assessment questions
- Count Card Duel RoundsOA · Seen Nov 2026
- Count Lamps Lighting Each PointOA · Seen Nov 2026
- Simulate Orchard Rot SpreadOA · Seen Nov 2026
- In-Memory File SystemOA · Seen Nov 2026
- Task Management SystemOA · Seen Nov 2026
- Recipe Management SystemOA · Seen Nov 2026
- Progressive Banking SystemOA · Seen Nov 2026
- Credit Accounts with Scheduled TransfersOA · Seen Oct 2026
- Parcel Attribute Store with TTLOA · Seen Oct 2026
- Parcel Event TrackingOA · Seen Oct 2026
Similar questions at other companies
- Build the Task HierarchyPendo · OA
- Lowest Number in an Open RangeCapital One · OACapital One interview questions
- Missing IntervalsIBM · OAIBM interview questions
- Total Cost Against the Lowest Earlier PricePayPal · OAPayPal interview questions
- Count User Mentions in Chat MessagesRamp · OARamp interview questions
- Count Valid Square PairsAuxoAI · OA
- Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Two SumAmazon · OA · PHONE SCREEN
- Competition Winner and Runner-Up (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
- Speed Up a Slow FunctionCitadel · OA · PHONE SCREENCitadel interview questions
Airbnb interview experiences
- Airbnb OA · November 2026
- Airbnb Campus and Full-Time SWE OA / Interviews: 9 Experiences
- Airbnb Intern Phone Interviews: 21 Experiences (Part 7 of 7)
- Airbnb Intern OA and Phone Screens: 17 Experiences (Part 6 of 7)
- Airbnb Intern OA and Onsite Rounds: 16 Experiences (Part 5 of 7)
- Airbnb Intern OA, Diplomacy Game: 19 Experiences (Part 4 of 7)