FastPrepRank Bipedal Dinosaurs by Speed

Rank Bipedal Dinosaurs by Speed

Meta logoMeta● MediumINTERNPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

You receive two arrays containing the lines of two CSV files. Each array includes its header as the first row.

  • dataset1 has header NAME,LEG_LENGTH,DIET.
  • dataset2 has header NAME,STRIDE_LENGTH,STANCE.

Join rows by an exact NAME match. Keep only matching dinosaurs whose STANCE is bipedal. For each retained dinosaur, calculate:

speed = ((STRIDE_LENGTH / LEG_LENGTH) - 1) * sqrt(LEG_LENGTH * 9.8)

Return the retained dinosaur names from fastest to slowest. Decimal measurements are interpreted exactly. If two mathematical speeds are equal, place the lexicographically smaller name first.

Function

rankBipedalDinosaurs(dataset1: String[], dataset2: String[]) → String[]

Examples

Example 1

dataset1 = ["NAME,LEG_LENGTH,DIET","Hadrosaurus,1.4,herbivore","Struthiomimus,0.72,omnivore","Velociraptor,1.8,carnivore","Tyrannosaurus Rex,6.5,carnivore"]dataset2 = ["NAME,STRIDE_LENGTH,STANCE","Tyrannosaurus Rex,4.76,bipedal","Hadrosaurus,1.3,bipedal","Struthiomimus,1.24,bipedal","Velociraptor,2.62,bipedal","Euoplocephalus,1.97,quadrupedal"]return = ["Struthiomimus","Velociraptor","Hadrosaurus","Tyrannosaurus Rex"]

All four returned names have matching rows and a bipedal stance. Their calculated speeds decrease in the returned order.

Example 2

dataset1 = ["NAME,LEG_LENGTH,DIET","alpha,1,herbivore","beta,4,carnivore","gamma,9,omnivore"]dataset2 = ["NAME,STRIDE_LENGTH,STANCE","alpha,2,bipedal","beta,8,quadrupedal","ghost,100,bipedal","gamma,18,bipedal"]return = ["gamma","alpha"]

beta is quadrupedal and ghost has no matching leg record. Although alpha and gamma have the same stride-to-leg ratio, gamma has the greater speed under the complete formula.

Example 3

dataset1 = ["NAME,LEG_LENGTH,DIET","zeta,1,omnivore","alpha,1,herbivore"]dataset2 = ["NAME,STRIDE_LENGTH,STANCE","zeta,2,bipedal","alpha,2,bipedal"]return = ["alpha","zeta"]

The two calculated speeds are equal, so the names use lexicographic order.

Example 4

dataset1 = ["NAME,LEG_LENGTH,DIET","solo,2,herbivore"]dataset2 = ["NAME,STRIDE_LENGTH,STANCE","solo,3,quadrupedal"]return = []

The only matched dinosaur is not bipedal, so the result is empty.

Constraints

  • 1 <= dataset1.length, dataset2.length <= 2 * 10^5 + 1, including the header row.
  • Every data row has exactly three comma-separated fields, and names contain no commas.
  • Names are nonempty and unique within each dataset. Each name contains only ASCII letters, digits, internal spaces, hyphens, or underscores. CSV fields have no surrounding whitespace.
  • LEG_LENGTH and STRIDE_LENGTH are positive decimal numbers no greater than 10^6, written as one or more digits, optionally followed by a decimal point and one to six digits. A value below one includes its leading zero, for example 0.5; signs and exponent notation are not used.
  • Rows without an exact name match are ignored. If no matching bipedal dinosaur exists, return an empty array.

More Meta problems

See Meta hiring insights
public String[] rankBipedalDinosaurs(String[] dataset1, String[] dataset2) {
    // write your code here
}
dataset1["NAME,LEG_LENGTH,DIET","Hadrosaurus,1.4,herbivore","Struthiomimus,0.72,omnivore","Velociraptor,1.8,carnivore","Tyrannosaurus Rex,6.5,carnivore"]
dataset2["NAME,STRIDE_LENGTH,STANCE","Tyrannosaurus Rex,4.76,bipedal","Hadrosaurus,1.3,bipedal","Struthiomimus,1.24,bipedal","Velociraptor,2.62,bipedal","Euoplocephalus,1.97,quadrupedal"]
expected["Struthiomimus", "Velociraptor", "Hadrosaurus", "Tyrannosaurus Rex"]
Checking account…