Bloomberg Interview Question

round1: reverse Integer, Valid Parentheses(leetcode 20) (two interviewers) round2: 1.design a horse racing system 2. given a height matrix, decide whether the rain can flow form the inside column to the left and right margin(two interviewers) round 3: talk with a manger, some behavior question, how to find the kth largest number in an unsorted array. round 4: talk with HR, behavior question.

Interview Answers

Anonymous

Aug 20, 2017

//2.1 Horse racing system class Horse { public: int position; int end; Horse(): position(0){} void ready(int distance) { position = 0; end = distance; } bool move() { int go = rand() % 3; position += go; return (position >= end); } }; class Race { public: vector horses; int distance; Race(int horsesN, int inDistance) :horses(horsesN), distance(inDistance) { } void newRace() { //ready for(int i = 0; i < horses.size(); i++) { horses[i].ready(distance); cout << i << " " ; for(int j = 0; j < (distance + 4); j++) { if(j == horses[i].position) { cout << "*"; } else if(j == distance) { cout << "|"; } else { cout << "-"; } } cout << endl; } cout << endl; //go bool winner = false; int winnerIdx; int winnerEnd = 0; while(!winner) { for(int i = 0; i < horses.size(); i++) { bool finish; finish = horses[i].move(); cout << i << " " ; for(int j = 0; j < (distance + 4); j++) { if(j == horses[i].position) { cout << "*"; } else if(j == distance) { cout << "|"; } else { cout << "-"; } } cout << endl; if(finish && winnerEnd < horses[i].position) { winner = true; winnerIdx = i; winnerEnd = horses[i].position; } } cout << endl; } cout << "Winner: " << winnerIdx << endl; } };

Anonymous

Aug 20, 2017

//1.1 Reverse integer int reverseInt(int inN) { stack digits; while(inN != 0) { int newDigit = inN % 10; digits.push(newDigit); inN = inN / 10; } int sol = 0; int pos = 1; while(!digits.empty()) { int newDigit = digits.top(); digits.pop(); sol += (newDigit * pos); pos *= 10; } return sol; } int reversIntString(int inN) { string strN = to_string(inN); string revN; for(int i = strN.size() - 1; i >= 0; i--) { revN.push_back(strN[i]); } return stoi(revN); }