├── README.md ├── Faulty Keyboard: Problem Statement ├── Faulty-Keyboard:Solution.cpp ├── College Applications: Problem Statement └── CollegeApps-Solution.cpp /README.md: -------------------------------------------------------------------------------- 1 | # GoldmanSachs-Coding-Challenge 2 | The problems (and my solutions to them) for my hacker rank assessment for the Goldman Sachs Software Engineering Internship for 2019 Summer 3 | -------------------------------------------------------------------------------- /Faulty Keyboard: Problem Statement: -------------------------------------------------------------------------------- 1 | The 'a' key on the keyboard is not working correctly. Instead of typing 'a' or 'A', it toggles the CapsLock. 2 | Return the value of a string that is typed into a keyboard on such a faulty keyboard. 3 | Note: If shift is pressed when CapsLock is on characters will be printed in lower case 4 | 5 | e.g. 6 | Input: "Help, my keyboard is not working" 7 | Output: "Help, my keyboRD IS NOT WORKING" 8 | 9 | Input: "Hi my name is Adam Levine" 10 | Output: "Hi my nME IS dM lEVINE" 11 | -------------------------------------------------------------------------------- /Faulty-Keyboard:Solution.cpp: -------------------------------------------------------------------------------- 1 | //Input and output was handled by Hacker Rank 2 | //Testees had to only complete a single function that was passed in the string typed in and would return the output on the faulty keyboard 3 | #incude //Extra include statement required for islower, toupper etc. functions 4 | 5 | string faultyKeyboard(string input) 6 | { 7 | bool CapsLockOn = false; 8 | 9 | for (int i = 0; input[i] != '\0';) 10 | { 11 | if (input[i] == 'a' || input[i] == 'A') 12 | { 13 | CapsLockOn = ! CapsLockOn; 14 | input.erase(i, 1); 15 | continue; 16 | } 17 | 18 | if (CapsLockOn) 19 | { 20 | if (islower(input[i])) 21 | input[i] = toupper(input[i]); 22 | else 23 | input[i] = tolower(input[i]); 24 | } 25 | 26 | i++; 27 | } 28 | 29 | return input; 30 | } 31 | -------------------------------------------------------------------------------- /College Applications: Problem Statement: -------------------------------------------------------------------------------- 1 | In country X, colleges pick students strictly based on their scores i.e. students with higher scores get first pick. 2 | All students have a list of preferences of colleges that they would like to attend, and if on their turn, the number 3 | of seats left in their top choice is 0, they apply to their next choice - so on and so forth, in case none of the 4 | colleges on a student's preference list has seats left (on their turn), the student doesn't go to college. 5 | Colleges have a fixed number of seats. 6 | 7 | The number of students, their test scores, the number of colleges, the number of seats available at each college 8 | and the preference list of every student will be provided. 9 | 10 | Input: 11 | NumberOfStudents 12 | Score1 13 | Score2 14 | ... 15 | NumberOfColleges 16 | Seats available at College 1 17 | Seats available at College 2 18 | ... 19 | NumberOfPreferencesInPrefenceList 20 | CollegePick1 CollegePick2 ... 21 | CollegePick1 CollegePick2 ... 22 | ... 23 | -------------------------------------------------------------------------------- /CollegeApps-Solution.cpp: -------------------------------------------------------------------------------- 1 | // Input and output was handled by Hacker Rank 2 | // Testees had to only complete a single function that was passed in: vector collegeSeatsArray, vector studentScoresArray, 3 | // vector> studentCollegePreferencesArray 4 | // The indices of the vector collegeSeatsArray indicate the college number - used to reference the college in the 5 | // studentCollegePreferencesArray. The indices of the studentScoresArray correspond to their corresponding preferences vector in the 6 | // studentCollegePreferencesArray. The results had to be returned in a vector where the first element the number of seats left, 7 | // and the second element indicated the number of students who didn't go to college 8 | // Each student only gets allocated to one college. 9 | 10 | // Helper functions 11 | int sumElements(vector vec) 12 | { 13 | int sum = 0; 14 | for (int i = 0; i < vec.size(); i++) 15 | sum += vec[i]; 16 | return sum; 17 | } 18 | 19 | vector uniqueSort(vector vec) 20 | { 21 | sort(vec.begin(), vec.end()); 22 | vec.erase(unique(vec.begin(), vec.end()), vec.end()); 23 | return vec; 24 | } 25 | 26 | // The method used in this function ensures that in cases where students have the same scores, the student whose score 27 | // was mentioed earlier in the array will get priority 28 | vector allocate(vector collegeSeatsArray, vector studentScoresArray, vector> studentCollegePreferencesArray) 29 | { 30 | int seatsLeft = sumElements(collegeSeatsArray); 31 | int studentsUnallocated = studentScoresArray.size()' 32 | 33 | unordered_map>> scoresAndPreferences; 34 | for (int i = 0; i < studentScoresArray.size(); i++) 35 | { 36 | if (scoresAndPreferences.find(studentScoresArray[i]) != scoresAndPreferences.end()) 37 | scoresAndPreferences[studentScoresArray[i]] = vector> preferences {studentCollegePreferencesArray[i]}; 38 | else 39 | { 40 | vector> preferences = scoresAndPreferences[studentScoresArray[i]]; 41 | preferences.push_back(studentCollegePreferencesArray[i]); 42 | scoresAndPreferences[studentScoresArray[i]] = preferences; 43 | } 44 | } 45 | 46 | studentScoresArray = uniqueSort(studentScoresArray); 47 | 48 | for (int i = 0; i < studentScoresArray.size(); i++) 49 | { 50 | vector> currentStudentPreferences = scoresAndPreferences[studentScoresArray[i]]; 51 | for (int j = 0; j < currentStudentPreferences.size(); j++) 52 | for (int k = 0; k < currentStudentPreferences[j].size(); k++) 53 | { 54 | if (currentStudentPreferences[j][k] > 0) 55 | { 56 | currentStudentPreferences[j][k]--; 57 | studentsUnallocated--; 58 | seatsLeft--; 59 | break; 60 | } 61 | } 62 | } 63 | 64 | return vector results {seatsLeft, studentsUnallocated}; 65 | } 66 | --------------------------------------------------------------------------------