Smallest Merge of Two Strings
Updated
Problem statement
Build a new string from word1 and word2. At each step, remove the first letter of either word (one that is not empty yet) and add it to the end of the new string. Keep going until both words are empty, so every letter is used once and each word's letters keep their order.
Return the smallest string, in dictionary order, that can be built this way.
Function
smallestMerge(word1: String, word2: String) → StringExamples
Example 1
word1 = "tug"word2 = "map"return = "maptug"Each step takes the smaller front letter: m (from map), a, p, then only tug is left. The result is "maptug".
Example 2
word1 = "k"word2 = "ki"return = "kik"Both words start with k. Taking it from "ki" first leaves "k" and "i", so i comes next: "kik". Taking it from "k" first would give "kki", which is larger because k comes after i.
Example 3
word1 = "sun"word2 = "sum"return = "ssumun"The words agree on s and u and then differ, and m comes before n. So the first s is taken from "sum". Next comes the s of "sun", then u and m from "sum", and last the leftover un: "ssumun".
Constraints
1 <= word1.length, word2.length <= 2000- Both words contain only lowercase English letters.
More Capital One Online Assessment questions
- Rank Football TeamsOA · Seen Oct 2026
- Count Subarrays Matching a Comparison PatternOA · Seen Oct 2026
- Arithmetic Triplets After QueriesOA · Seen Oct 2026
- Branch with the Most FilesOA · Seen Oct 2026
- Count One-Swap Number PairsOA · Seen Oct 2026
- Distinct Values on Maximum-Sum FramesOA · Seen Oct 2026
- Earliest Meeting Time for All EmployeesOA · Seen Oct 2026
- Earn the Most 🐝OA · Seen Oct 2026
- Sort Adjacent PairsOA · Seen Oct 2026
- Bouncing Diagonal WeightsOA · Seen Oct 2026
Similar questions at other companies
- Minimum Swaps to Make PalindromeAmazon · OA
- Missing WordsVanguard · OAVanguard interview questions
- Shortest Way to Form a Target StringPinterest · PHONE SCREENPinterest interview questions
- Wildcard Matching with StarsConfluent · PHONE SCREENConfluent interview questions
- Text Pattern Matching with StarsHeadlands Technologies · PHONE SCREENHeadlands Technologies interview questions
- Longest Spoken PrefixElevenLabs · ONSITE INTERVIEWElevenLabs interview questions
- Palindrome After At Most One DeletionAdobe · PHONE SCREENAdobe interview questions
- Minimum No-Repeat Segments After One Character RemovalSalesforce · OASalesforce interview questions
- Maximum Easy Tasks After RearrangingIBM · OAIBM interview questions
- Longest Non-Decreasing Substring After Pair SwapsVisa · OAVisa interview questions
Capital One interview 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
- Capital One TDP Agentic Power Day: 25 Experiences (Part 1 of 2)