FastPrepSort Objects by a Field
Problems/Sort Objects by a Field

Sort Objects by a Field

Zillow logoZillow● EasyFULLTIMEOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported at Zillow in 2016 and 2018. A public code repository from October 2016 holds a Java answer to a Zillow coding challenge: sort a collection of Person objects by a field picked by its name, plus a write-up that compares designs. A Senior Software Engineer candidate in San Francisco in August 2018 was sent a take-home that had to use Java 8 after a recruiter reached out; the task was to write Java code that puts objects in order by one of their fields and to explain the trade-offs.

The 2016 repository's write-up shows that a Person has an ssn field. Neither the write-up nor the 2018 report lists the other fields, says which direction the sort goes or says how equal values are ordered, and neither report names a test platform. The 2016 repository does not use the word take-home.

These choices are ours: a Person has the fields firstName, lastName, age and ssn; each person reaches the function as a list of four strings, and the field as its name; ages compare as numbers and the other fields as text; the sort goes from smallest to largest and keeps people with equal values in input order; there are 1 to 200 people; names have 1 to 10 letters, ages run from 0 to 120, and every ssn is written as ddd-dd-dddd and differs from person to person; the task can be solved in five languages instead of Java 8 alone. The 2018 report places the task in a take-home, and we read the 2016 coding challenge as one too, so we list the task as an online assessment. The trade-offs are discussed in the editorial; the tests check the sorted order.

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

A Person record has four fields, always in this order: firstName, lastName, age and ssn (a U.S. Social Security number). Each entry of people is one person, written as a list of four strings that hold these fields.

The string field names one of the four fields. Return the people sorted by their value in that field, from smallest to largest:

  • firstName and lastName are compared in alphabetical order.
  • age is compared as a number, so "9" comes before "27".
  • Every ssn is written as ddd-dd-dddd (3 digits, a dash, 2 digits, a dash, 4 digits), so comparing two of them as text gives their numeric order.

When two people have the same value in that field, keep them in the order they had in people. A sort that keeps equal items in their original order is called stable.

Function

sortByField(people: List<List<String>>, field: String) → List<List<String>>

Examples

Example 1

people = [["Maya","Chen","34","912-34-5678"],["Liam","Ortiz","9","905-11-2233"],["Ava","Brooks","34","931-20-4455"],["Noah","Kim","27","948-02-7719"]]field = "age"return = [["Liam","Ortiz","9","905-11-2233"],["Noah","Kim","27","948-02-7719"],["Maya","Chen","34","912-34-5678"],["Ava","Brooks","34","931-20-4455"]]

As numbers, the ages are 9, 27, 34 and 34, so Liam comes first even though the text "9" would sort after "27". Maya and Ava are both 34, so they stay in their input order: Maya, then Ava.

Example 2

people = [["Zoe","Smithers","41","977-50-1203"],["Eli","Smith","29","963-08-3341"],["Ian","Baker","35","920-77-0019"]]field = "lastName"return = [["Ian","Baker","35","920-77-0019"],["Eli","Smith","29","963-08-3341"],["Zoe","Smithers","41","977-50-1203"]]

Baker comes first. "Smith" is the start of "Smithers", and a shorter text that is the start of a longer one comes first, so Eli Smith comes before Zoe Smithers.

Example 3

people = [["Omar","Reyes","52","941-22-0001"],["Lena","Park","19","903-99-9999"]]field = "ssn"return = [["Lena","Park","19","903-99-9999"],["Omar","Reyes","52","941-22-0001"]]

The first 3 digits already differ: 903 is less than 941, so Lena comes first.

Constraints

  • 1 <= people.length <= 200
  • people[i].length == 4
  • Each firstName and lastName has 1 to 10 English letters: an uppercase first letter, then lowercase letters.
  • Each age is a whole number from 0 to 120, written without leading zeros.
  • Each ssn has the form ddd-dd-dddd, where every d is a digit, and no two people share an ssn.
  • field is one of "firstName", "lastName", "age" and "ssn".

More Zillow interview questions

  • Cumulative Fibonacci SumOA · PHONE SCREEN · Seen Aug 2021
  • First Subarray with the Largest SumOA · ONSITE INTERVIEW · Seen Aug 2021
  • Reverse StringOA · PHONE SCREEN · Seen Sep 2019
  • Mastermind Guess ScoreOA · PHONE SCREEN · Seen Jun 2019
  • Convert a String to a LongOA · Seen Jan 2018
  • Insert and Delete in a Trinary TreeOA · Seen May 2016
  • Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
  • Time Based Key-Value StorePHONE SCREEN · Seen Feb 2024
  • Filter Formatted Records by CriteriaPHONE SCREEN · Seen Sep 2023
  • Build a Hash TablePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023

Similar questions at other companies

  • Count User Mentions in Chat MessagesRamp · OARamp interview questions
  • Word Frequency Counts in Alphabetical OrderAbridge · OAAbridge interview questions
  • Top N Longest SentencesDeloitte · OADeloitte interview questions
  • Maximize Workday EarningsCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions
  • Frequently Bought TogetherAmazon · OA
  • Aggregate Paginated Project Run RecordsReplit · OA
  • Product Record SearchWells Fargo · OAWells Fargo interview questions
  • Sort Ancestral NamesRoblox · OARoblox interview questions
  • Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREENCrunchyroll interview questions
  • Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
All Zillow interview questionsSee Zillow hiring insights
Saved
class Solution {
    public List<List<String>> sortByField(List<List<String>> people, String field) {
        // Write your code here.
    }
}
Checking account…