FastPrepDecode Words with Anagram Keys
Problems/Decode Words with Anagram Keys

Decode Words with Anagram Keys

Cresta logoCresta● MediumFULLTIMEOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Cresta online assessment. The report describes words with their letters rearranged and some conflicting notes about ambiguity. Our version fixes that the first letter stays in place and that ties go by dictionary order. We do not claim that the workaround in the report passed the hidden tests. Our version is about 70% the same as the reported question.

You are given a list of lowercase knownWords and an encodedMessage. For this exercise, assume each encoded token is a rearrangement of a known word’s letters that preserves that word’s first letter.

Decode each token independently. A word is a candidate exactly when it has the same first letter and the same number of every letter as the token. If several known words are candidates, choose the one that comes first alphabetically. Listing the same known word more than once does not change the choices.

Every nonempty message token is guaranteed to have at least one candidate. The message is empty or contains nonempty tokens separated by single spaces, with no leading or trailing space. Return the decoded words joined by single spaces, or the empty string for an empty message.

Function

decodeMessage(knownWords: String[], encodedMessage: String) → String

Examples

Example 1

knownWords = ["cat","dog"]encodedMessage = "cta dgo"return = "cat dog"

The tokens preserve c and d and decode to cat dog.

Example 2

knownWords = ["east","eats","tea"]encodedMessage = "etsa tae"return = "east tea"

Both east and eats match the first token; choose east. The second token decodes to tea.

Example 3

knownWords = []encodedMessage = ""return = ""

An empty message has no tokens to decode.

Constraints

  • 0 <= knownWords.length <= 2000.
  • Each known word and encoded token contains 1..40 lowercase English letters.
  • The total number of letters in the dictionary is at most 8 × 10^4.
  • The message contains at most 2000 tokens and at most 10^5 characters including spaces.

More Cresta interview questions

  • Find a File in a Directory SnapshotOA · Seen Oct 2026
  • Leaves Remaining After WindOA · Seen Oct 2026
  • Maximum Suffix–Prefix OverlapOA · Seen Oct 2026
  • Particle Moves to TargetOA · Seen Jul 2026
  • Rightward Bacteria ConsumptionOA · Seen Jul 2026
  • Top Three Companies by ProfitOA · Seen Jul 2026
  • Peak Chat Load Across AgentsPHONE SCREEN · Seen Nov 2026
  • File Storage ServicePHONE SCREEN · Seen Nov 2026
  • Dynamic Least-Loaded Job DispatcherPHONE SCREEN · Seen Oct 2026
  • Feature Flag Evaluation EngineONSITE INTERVIEW · Seen Jul 2026

Similar questions at other companies

  • Longest String ChainVisa · OAVisa interview questions
  • Frequently Bought TogetherAmazon · OA
  • Maximize Palindromic WordsSquarepoint Capital · OASquarepoint Capital interview questions
  • Aggregate Paginated Project Run RecordsReplit · OA
  • Parse and Filter Chargeback DisputesStripe · OAStripe interview questions
  • Count User Logins 🌿 (Singapore)PayPal · OAPayPal interview questions
  • Largest Lexicographical Substring 🌿Google · OAGoogle interview questions
  • Count Distinct Message MentionsZipRecruiter · OAZipRecruiter interview questions
  • Rank Common Translation MistakesDuolingo · OADuolingo interview questions
  • Aggregate Investment Ownership from CSVCarta · OA · PHONE SCREEN

Cresta interview experiences

  • Cresta Senior SWE Coding Round Interview Experience: 1 Experience
All Cresta interview questionsSee Cresta hiring insights
Saved
class Solution {
    public String decodeMessage(String[] knownWords, String encodedMessage) {
        // write your code here
    }

}
Checking account…