Reverse K-Groups, Then Swap Adjacent Groups
Updated
Problem statement
Walk along the list that starts at head and cut it into blocks: the first k nodes, then the next k, and so on. If fewer than k nodes are left over at the end, they form a tail.
- Turn every full block around, so that its last node comes first. The tail is not turned around.
- Then take the full blocks two at a time (first and second, third and fourth, and so on) and let each pair trade places. If the number of full blocks is odd, the last full block has no partner and does not move. The tail always stays at the very end.
Give back the node that now comes first.
Function
reverseAndSwapGroups(head: ListNode, k: int) → ListNodeExamples
Example 1
head = [1,2,3,4,5]k = 2return = [4,3,2,1,5]Groups of 2: [1, 2], [3, 4] and the short group [5]. Reversing each full group gives 2 → 1 → 4 → 3 → 5. Swapping the first two groups gives 4 → 3 → 2 → 1 → 5. The short group [5] stays at the end.
Example 2
head = [1,2,3,4,5,6]k = 2return = [4,3,2,1,6,5]Reversing the three groups gives 2 → 1 → 4 → 3 → 6 → 5. The first and second groups swap places. The third group has no partner, so it stays: 4 → 3 → 2 → 1 → 6 → 5.
Example 3
head = [30,10,20,60,50,40,80,70,90,15,25]k = 3return = [40,50,60,20,10,30,90,70,80,15,25]With k = 3, the full groups reverse to [20, 10, 30], [40, 50, 60] and [90, 70, 80]. The first two swap. The third has no partner and stays in place. The short group [15, 25] keeps its order and stays last.
Constraints
- The number of nodes
nis between1and2000. 1 <= k <= n-1000 <= Node.val <= 1000
More Capital One Onsite Interview questions
- Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
- Sort ColorsONSITE INTERVIEW · Seen Oct 2026
- Virtual Card Transaction ValidatorONSITE INTERVIEW · Seen Oct 2026
- Banking System with Top ActivityONSITE INTERVIEW · Seen Sep 2026
- Transaction Event StoreONSITE INTERVIEW · Seen Jul 2026
- Shortest Path in an Unweighted GraphONSITE INTERVIEW · Seen Jul 2026
- Duplicate Transactions Within 60 SecondsONSITE INTERVIEW · Seen May 2026
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
- Cashback TrackerONSITE INTERVIEW · Seen Nov 2025
- Reverse a StringOA · ONSITE INTERVIEW · Seen Oct 2025
Similar questions at other companies
- LFU CacheAmazon · ONSITE INTERVIEW
- LFU CacheJP Morgan Chase · ONSITE INTERVIEWJP Morgan Chase interview questions
- LFU CacheSnowflake · OA · ONSITE INTERVIEWSnowflake interview questions
- LRU Cache with TTLAutodesk · ONSITE INTERVIEWAutodesk interview questions
- Recover a Function-Call LRU Cache with a Write-Ahead LogAnthropic · ONSITE INTERVIEWAnthropic interview questions
- Merge k Sorted ListsZS · ONSITE INTERVIEWZS interview questions
- LFU CacheSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Max StackLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- LFU CacheBlinkit · ONSITE INTERVIEWBlinkit interview questions
- Fair Multi-Tenant CacheOkta · ONSITE INTERVIEWOkta interview questions
Capital One interview experiences
- Capital One Coding OA Questions: 3 Experiences
- Capital One Experienced Engineer Power Day and OA: 6 Experiences
- Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
- Capital One TDP Agentic Round (AI Coding): 7 Experiences
- Capital One SWE Power Day, Level Not Reported: 6 Experiences
- Capital One TIP Intern OA and Power Day: 13 Experiences