Problem · Array

Deterministic Secret Santa Assignment

Learn this problem
EasyShopify logoShopifyFULLTIMEPHONE SCREEN

Problem statement

You are given a matrix participants. Each row contains exactly two strings: [name, email].

Build the deterministic Secret Santa assignment as follows:

  1. Sort the participants by email address in ascending lexicographic order.
  2. For each sorted participant, assign the next participant in that order as the recipient.
  3. The final participant assigns a gift to the first participant.

Return a matrix whose rows are [giverEmail, recipientEmail], in ascending order of giverEmail.

Function

assignSecretSanta(participants: String[][]) → String[][]

Examples

Example 1

participants = [["Mina","mina@example.com"],["Ari","ari@example.com"],["Zoe","zoe@example.com"]]return = [["ari@example.com","mina@example.com"],["mina@example.com","zoe@example.com"],["zoe@example.com","ari@example.com"]]

The sorted email order is ari, mina, zoe. Each giver receives the next email, and the last wraps to the first.

Example 2

participants = [["Ben","b@shop.test"],["Ana","a@shop.test"]]return = [["a@shop.test","b@shop.test"],["b@shop.test","a@shop.test"]]

With two participants, each one is assigned to the other.

Example 3

participants = [["Kai","kai@x.dev"],["Kai","a@x.dev"],["Lee","m@x.dev"],["Rae","z@x.dev"]]return = [["a@x.dev","kai@x.dev"],["kai@x.dev","m@x.dev"],["m@x.dev","z@x.dev"],["z@x.dev","a@x.dev"]]

Names do not determine identity or order; unique email addresses do.

Constraints

  • 2 <= participants.length <= 10^5
  • participants[i].length == 2
  • Every name and email is a non-empty ASCII string of at most 100 characters.
  • All email addresses are unique.

More Shopify problems

drafts saved locally
public String[][] assignSecretSanta(String[][] participants) {
    // Write your code here.
}
participants[["Mina","mina@example.com"],["Ari","ari@example.com"],["Zoe","zoe@example.com"]]
expected[["ari@example.com", "mina@example.com", "mina@example.com", "zoe@example.com", "zoe@example.com", "ari@example.com"]]
checking account