Pergunta de entrevista da empresa ODAIA

// Given a list postal codes, find the FSA (Forward Sortation Code) code that occurs most frequently. // input: // ["M5V 1W5", "L7Q 1T3", "L7M 1W1", "M5V 6X1", "M5V 2X1", "L7M 1W6"] // Output: // "M5V" Second Revision: They told me to support the US portal code. // Given a list postal codes, find the FSA (Forward Sortation Code) code that occurs most frequently. // input: // ["M5V 1W5", "L7Q 1T3", "L7M 1W1", "M5V 6X1", "M5V 2X1", "L7M 1W6", "42015", "11005", "41450", "11026"] // Output: // "M5V" Third Revision: They told me to return multiple values. If multiple values have the same occurrence. // Given a list postal codes, find the FSA (Forward Sortation Code) code that occurs most frequently. // input: // ["M5V 1W5", "L7Q 1T3", "L7M 1W1", "M5V 6X1", "M5V 2X1", "L7M 1W6", "42015", "11005", "41450", "11026", "42016", "42017"] // Output: // ["M5V", "420"]

Resposta da entrevista

Sigiloso

23 de fev. de 2023

Class FsaUtil{ /* * Get most frequent fsa from postal code list */ public static List getMostFrequentFSA(List postcalCodeList){ Map fsaMap = getFSAFromPostalCode(postcalCodeList); Integer maxCount = 0; String mostFrequentFSAs = []; for(Map.Entry entry : fsaMap.getEntries()){ if(entry.getValue() >= maxCount){ maxCount = entry.getValue(); mostFrequentFSAs.push(entry.getKey()); } } return mostFrequentFSAs; } /* * Abstract all the FSAs from Postalcode list */ public static Map getFSAFromPostalCode(List postcalCodeList){ Map fsaMap = new HashMap(); for(String postalCode : postcalCodeList){ // I'm assuming it's valid postcode so I'm not checking for array out of bound exception String fsa = postalCode.subString(0, 3); // Set count for FSA map.put(fsa, map.getOrDefault(fsa, 0) + 1); } return fsaMap; } }