FastPrepReverse K-Groups, Then Swap Adjacent Groups
Problems/Reverse K-Groups, Then Swap Adjacent Groups

Reverse K-Groups, Then Swap Adjacent Groups

Capital One logoCapital One● HardNEW GRADINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Three Capital One reports from 2021 and 2022 describe this two-step list question in the final round. One is from a TDP associate software engineer and two are from interns. The most detailed one gives the steps and two worked examples with k = 2. The steps are to reverse each group of k nodes, then swap neighbouring groups. Its two examples are used here as Examples 1 and 2.

Those examples show that a last full group with no partner stays in place. They never show a leftover tail of two or more nodes. We chose to leave such a tail exactly as it is: it is neither reversed nor swapped. The reports give no limits, so the list size and value range are our choices too.

Our version is about 90% the same as the reported questions.

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.

  1. Turn every full block around, so that its last node comes first. The tail is not turned around.
  2. 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) → ListNode

Examples

Example 1

head = [1,2,3,4,5]k = 2return = [4,3,2,1,5]
head
12345
→
return
43215
Moved or new

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]
head
123456
→
return
432165
Moved or new

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]
head
3010206050408070901525
→
return
4050602010309070801525
Moved or new

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 n is between 1 and 2000.
  • 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
All Capital One interview questionsSee Capital One hiring insights
Saved
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseAndSwapGroups(ListNode head, int k) {
        // Write your code here.
    }
}
Checking account…