FastPrepSwap Adjacent in LR String
Problems/Swap Adjacent in LR String

Swap Adjacent in LR String

Google logoGoogle● MediumFULLTIMEONSITE INTERVIEW

Updated Dec 2026

QuestionExamples

Problem statement

Source note: Reported in a Google in-person onsite interview. The task was the well-known puzzle of turning one string of L, R and X characters into another, where an L may step left into an X and an R may step right into an X. After a working search that tried moves, the interviewer asked for something faster; the candidate reached quadratic time, and the interviewer hinted that linear time was possible.

The report names the task but gives no examples or limits. Here we use the usual limits for this task, lengths up to 10^4, and three examples of our own. The faster versions from the follow-up are covered in the editorial; the tests check only the true or false answer.

Our version is about 95% the same as the reported question.

You are given two strings start and result of the same length. Every character is 'L', 'R' or 'X'.

In one move you may do either of these:

  • replace one occurrence of "XL" with "LX", or
  • replace one occurrence of "RX" with "XR".

Return true if some sequence of moves (possibly none) turns start into result, and false otherwise.

The problem statement continues
Pro

Examples

Example 1

start = "XRXXLX"result = "XXRLXX"return = true

Move the R one step right, then the L one step left: "XRXXLX" becomes "XXRXLX" ("RX" to "XR"), then "XXRLXX" ("XL" to "LX").

02 · Example 2
FastPrep Pro
Reported in 1 Google interview this week

Unlock this recently reported problem

FastPrep Pro gives you full access to interview problems reported within the last week.

  • Full problem statement and constraints
  • 2 more worked examples, explained
  • Guided hints and editorial
  • Run your code on real test cases
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week

More Google Onsite Interview questions

  • Count Winning Dice Face PairsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Find a Failing Test PairONSITE INTERVIEW · Seen Nov 2026
  • Minimum Cars for Rental RequestsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
  • Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
  • Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Shortest Deletion for Distinct CharactersOracle · ONSITE INTERVIEWOracle interview questions
  • Validate a Palindrome After Limited DeletionsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
  • Reverse File LinesCitadel · ONSITE INTERVIEWCitadel interview questions
  • Longest Palindromic Substring LengthMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • All Distinct Palindromic SubstringsDoorDash · ONSITE INTERVIEWDoorDash interview questions
  • String CompressionBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Longest Palindromic SubstringPeopleStrong · ONSITE INTERVIEWPeopleStrong interview questions
  • Strings at Most One Edit ApartExpedia · ONSITE INTERVIEWExpedia interview questions
  • String CompressionShnoor International · ONSITE INTERVIEW
  • Longest Spoken PrefixElevenLabs · ONSITE INTERVIEWElevenLabs interview questions

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
CodePython 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import heapq
from collections import defaultdict
 
class Solution:
def solve(self, values, queries):
# Build the adjacency structure
n = len(values)
graph = defaultdict(list)
for u, v in queries:
graph[u].append(v)
 
best = [float("inf")] * n
heap = [(0, 0)]
while heap:
cost, node = heapq.heappop(heap)
if cost > best[node]:
continue
Run and Submit unlock with Pro
FastPrep Pro
Reported in 1 Google interview this week

Unlock this recently reported problem

FastPrep Pro gives you full access to interview problems reported within the last week.

  • Full problem statement and constraints
  • 2 more worked examples, explained
  • Guided hints and editorial
  • Run your code on real test cases
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week