Round 1
Questions:
-
791 Custom Sort String Link
public String customSortString(String order, String s) { Map<Character, Integer> map = new HashMap<>(); int n = s.length(); int m = order.length(); for(int i=0; i<n; i++) { char letter = s.charAt(i); map.put(letter, map.getOrDefault(letter,0)+1); } StringBuilder result = new StringBuilder(); for(int i =0; i<m; i++) { char letter = order.charAt(i); if(map.containsKey(letter)) { int count = map.get(letter); while(count-- > 0) { result.append(letter); map.put(letter,count); } } } for(char key : map.keySet()) { int count = map.get(key); while(count > 0) { result.append(key); count--; } } return result.toString(); }
-
Plan a round trip between 2 cities with minimum flight cost. Given 2 arrays - Departure costs and Return Costs. Both arrays will always have the same size. D = [10, 8, 9, 11, 7]
R = [8, 8, 10, 7, 9]
Ans = 15 (8+7); Note: It cannot be 7+7 as you can't take return flight without taking departure flight.public static int minFlightCost(int[] D, int[] R) { int n = D.length; int minDep = Integer.MAX_VALUE; int minRT = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { if (i > 0) { minRT = Math.min(minRT, minDep + R[i]); } minDep = Math.min(minDep, D[i]); } return minRT; }
Candidate's Approach
The candidate implemented the solutions for both questions using Java. For the first question, they created a frequency map for characters in the string s
and then built the result string based on the order specified in order
. For the second question, they maintained the minimum departure cost while iterating through the arrays to calculate the minimum round trip cost.
Interviewer's Feedback
No feedback provided.