FastPrepHighest-Ranked University by Country

Highest-Ranked University by Country

Agoda logoAgoda● EasyFULLTIMEOA
Learn

Problem statement

You are given university records collected from every page of a paginated ranking API. The records are represented by three parallel arrays:

  • universities[i] is the university name;
  • countries[i] is its country; and
  • rankDisplay[i] is its displayed rank, either a positive integer such as "12" or a positive range such as "601-650".

The numeric rank of a record is the first integer in rankDisplay[i]. A smaller rank is better.

Given targetCountry, return the name of its highest-ranked university. If several universities have the same best rank, return the lexicographically smallest name. Return the empty string when the country has no record.

Function

highestRankedUniversity(universities: String[], countries: String[], rankDisplay: String[], targetCountry: String) → String

Examples

Example 1

universities = ["Massachusetts Institute of Technology (MIT)","University of Oxford","University of Cambridge"]countries = ["United States","United Kingdom","United Kingdom"]rankDisplay = ["1","3","2"]targetCountry = "United Kingdom"return = "University of Cambridge"

The two matching universities have ranks 3 and 2. Rank 2 is better, so return "University of Cambridge".

Example 2

universities = ["Gamma Institute","Alpha University","Beta College"]countries = ["Canada","Canada","Canada"]rankDisplay = ["12","12","20"]targetCountry = "Canada"return = "Alpha University"

Gamma Institute and Alpha University tie at rank 12. The lexicographically smaller name is "Alpha University".

Example 3

universities = ["National University of Singapore (NUS)"]countries = ["Singapore"]rankDisplay = ["11"]targetCountry = "Thailand"return = ""

No record belongs to "Thailand", so return the empty string.

Constraints

  • 1 <= universities.length <= 10000
  • countries.length == universities.length
  • rankDisplay.length == universities.length
  • Every university name, country, and targetCountry is non-empty.
  • Every displayed rank is either one positive integer or a positive integer range a-b with a <= b.
  • Country matching is case-sensitive.

More Agoda problems

See Agoda hiring insights
public String highestRankedUniversity(String[] universities, String[] countries, String[] rankDisplay, String targetCountry) {
  // write your code here
}
universities["Massachusetts Institute of Technology (MIT)","University of Oxford","University of Cambridge"]
countries["United States","United Kingdom","United Kingdom"]
rankDisplay["1","3","2"]
targetCountry"United Kingdom"
expected"University of Cambridge"
Checking account…