Monthly Archives: June 2014
C++ || Yahoo! Sports – Current Batting Order – Programming Interview Test (Question 3 of 3)
The following is sample code which demonstrates a solution to the Yahoo! Sports programming interview question titled “Current Batting Order.”
The test was taken on November 2013 for educational purposes only. Though I completed the test, at the time I had no intention of actually applying for a position, which was for the Junior Backend Test Engineer.
Given two hours, the task was to solve a total of 3 programming questions. The following is question 3 of 3. Questions 1 and 2 can be found here and here.
=== 1. INSTRUCTIONS BEFORE TAKING TEST ===
1. Why take this test? If you get all 3 questions correct you are guaranteed an interview with a Sports Engineer.
2. This is a programming test. Before you begin, Please test your environment with a sample test here.
3. Please make sure you have time free before taking the test. The test usually takes 1 to 2 hours.
4. You are free to choose any language from the list and code.
5. You're expected to write the full code. All inputs are from STDIN and output to STDOUT. If you're using Java, use the classname as 'Solution'.
6. To understand more about the environment, time limits, etc. you can read the FAQ here.
7. You can print to console to debug your code using the appropriate print command for each language (Eg: cout for C++, printf for C, etc.).
=== 2. THE PROBLEM STATEMENT ===
Question 3 / 3 (Current Batting Order)
Given the batting rosters for multiple baseball games, please output the current batting order.
Input Format:
Game Id,Team Id,Player Id, Position, Batting Order, Sub Order
Sample Input (Comments are for explanation only. They won’t be part of actual test input):
# The away team lineup starts here
331028124,24,7912,CF,1,1
331028124,24,7631,2B,2,1
331028124,24,5909,1B,3,1
# Pitcher replaces 1B in batting order
331028124,24,8394,P,3,2
331028124,24,7245,LF,4,1
# The home team lineup starts here
331028124,17,9194,SS,1,1
The pitcher replaced the first baseman in the batting order as identified by the “Sub Order” column being greater than 1. The “Sub Order” column will increment for each substitution. The highest value of “Sub Order” identifies the current batter in the game.
When given a “real” data input set, the code should display the current 9 batters for the away team followed by those of the home team. Assume the away team lineup data is always encountered first. Also assume that you will get data for more than one game.
Sample Output:
331028124,24,7912,CF,1
331028124,24,7631,2B,2
331028124,24,8394,P,3
331028124,24,7245,LF,4
331028124,17,9194,SS,1
Sample testcases can be downloaded here, which contains sample input and correct output data.
=== 3. CURRENT BATTING ORDER ===
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
// ============================================================================ // Author: K Perkins // Date: Nov 26, 2013 // Updated: Jun 17, 2014 // Taken From: http://programmingnotes.org/ // File: Current-Batting-Order.cpp // Description: Demonstrates a solution to the Yahoo! Sports programming // interview test question titled "Current Batting Order." (3 of 3) // ============================================================================ #include <iostream> #include <vector> #include <string> #include <algorithm> #include <cstdlib> using namespace std; struct Players { int game_id; int team_id; int player_id; string pos; int bat_order; int sub_order; bool operator < (const Players& other) const { return sub_order < other.sub_order; } bool operator == (const Players& other) const { return game_id == other.game_id && team_id == other.team_id && bat_order == other.bat_order; } }; void process_batting_order(vector<Players>& stats, const vector<string>& tokens); void print(const vector<Players>& stats); vector<string> str_tok(const string& str, const string& delimiters); int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ vector<Players> stats; vector<string> tokens; string line; // read an entire line while(getline(cin, line)) { // break the line up into tokens according to the comma tokens = str_tok(line, ","); // process the data and save the processed stats into a vector process_batting_order(stats, tokens); } // print the data print(stats); cout << stats.size() << endl; return 0; }// end of main void process_batting_order(vector<Players>& stats, const vector<string>& tokens) { Players access; if(tokens.size() == 6) { access.game_id = atoi(tokens[0].c_str()); access.team_id = atoi(tokens[1].c_str()); access.player_id = atoi(tokens[2].c_str()); access.pos = tokens[3]; access.bat_order = atoi(tokens[4].c_str()); access.sub_order = atoi(tokens[5].c_str()); bool swap = false; // swap substitution order if we need to for(unsigned x = 0; x < stats.size(); ++x) { // if batting order is the same, and sub order for new // player is greater than the current player, swap // positions if((stats[x] == access) && (stats[x] < access)) { std::swap(stats[x], access); swap = true; break; } } if(!swap) { stats.push_back(access); } } }// end of process_batting_order void print(const vector<Players>& stats) { char c = ','; for(unsigned x = 0; x < stats.size(); ++x) { cout << stats[x].game_id << c << stats[x].team_id << c << stats[x].player_id << c << stats[x].pos << c << stats[x].bat_order << endl;//<< c << stats[x].sub_order<<endl; } }// end of print vector<string> str_tok(const string& str, const string& delimiters) { unsigned prev = 0; unsigned currentPos = 0; vector<string> tokens; // loop thru string until we reach the end while((currentPos = str.find_first_of(delimiters, prev)) != string::npos) { if(currentPos > prev) { tokens.push_back(str.substr(prev, currentPos - prev)); } prev = currentPos + 1; } // if characters are remaining, save to vector if(prev < str.length()) { tokens.push_back(str.substr(prev, string::npos)); } return tokens; }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
Sample testcases can be downloaded here, which contains sample input and correct output data.
You can read input into the program from a text file using I/O redirection from the Windows command prompt (CMD). Click here if you dont know how to do this.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Once compiled, you should get this as your output:
Sample Input (input002.txt):
330805124,19,6870,LF,1,1
330805124,19,6899,2B,2,1
330805124,19,7054,1B,3,1
330805124,19,9341,RF,4,1
330805124,19,7710,CF,5,1
330805124,19,8373,C,6,1
330805124,19,6698,3B,7,1
330805124,19,6793,SS,8,1
330805124,19,7257,P,9,1
330805124,24,8953,2B,1,1
330805124,24,6132,RF,2,1
330805124,24,8649,1B,3,1
330805124,24,7311,LF,4,1
330805124,24,8402,3B,5,1
330805124,24,8717,CF,6,1
330805124,24,8938,C,7,1
330805124,24,8831,SS-3B,8,1
330805124,24,7048,P,9,1
330805124,24,9070,PH,9,2
330805124,19,8442,P,9,2
330805124,24,9426,P,9,3
330805124,19,9306,P,1,2
330805124,19,7567,LF,9,3
330805124,24,8934,PR-SS,5,2
330805124,24,9383,P,9,4
330805124,24,9100,PH,9,5
Sample Output:
330805124,19,9306,P,1
330805124,19,6899,2B,2
330805124,19,7054,1B,3
330805124,19,9341,RF,4
330805124,19,7710,CF,5
330805124,19,8373,C,6
330805124,19,6698,3B,7
330805124,19,6793,SS,8
330805124,19,7567,LF,9
330805124,24,8953,2B,1
330805124,24,6132,RF,2
330805124,24,8649,1B,3
330805124,24,7311,LF,4
330805124,24,8934,PR-SS,5
330805124,24,8717,CF,6
330805124,24,8938,C,7
330805124,24,8831,SS-3B,8
330805124,24,9100,PH,9
C++ || Yahoo! Sports – Top 10 Fantasy Players – Programming Interview Test (Question 2 of 3)
The following is sample code which demonstrates a solution to the Yahoo! Sports programming interview question titled “Top 10 Fantasy Players.”
The test was taken on November 2013 for educational purposes only. Though I completed the test, at the time I had no intention of actually applying for a position, which was for the Junior Backend Test Engineer.
Given two hours, the task was to solve a total of 3 programming questions. The following is question 2 of 3. Questions 1 and 3 can be found here and here.
=== 1. INSTRUCTIONS BEFORE TAKING TEST ===
1. Why take this test? If you get all 3 questions correct you are guaranteed an interview with a Sports Engineer.
2. This is a programming test. Before you begin, Please test your environment with a sample test here.
3. Please make sure you have time free before taking the test. The test usually takes 1 to 2 hours.
4. You are free to choose any language from the list and code.
5. You're expected to write the full code. All inputs are from STDIN and output to STDOUT. If you're using Java, use the classname as 'Solution'.
6. To understand more about the environment, time limits, etc. you can read the FAQ here.
7. You can print to console to debug your code using the appropriate print command for each language (Eg: cout for C++, printf for C, etc.).
=== 2. THE PROBLEM STATEMENT ===
Question 2 / 3 (Top 10 Fantasy Players)
Given a stream of data that contains football player statistical data, where each line represents a single player followed by several stats for that player depicted as:
Player Id,Stat A,Stat B,Stat C,Stat D
Sample Input:
9533,5,45,0,0
8573,10,120,1,0
5352,2,22,0,1
8326,1,3,1,2
9311,12,120,2,1
3343,7,20,220,0
6343,6,18,170,0
7243,2,5,100,1
Write a program that displays the top 10 players with the greatest fantasy points based on statistical performance (in descending order of fantasy points) at any point in time. The program should display a single line of output that lists the player id along with the score for each player.
The program should print the following to standard out for each top 10 player when the input is ‘PRINT’ instead of statistical data for a player:
Player Id,Score
Use the following to score players:
2 pts for each Stat A
1 pt for each Stat B
6 pts for each Stat C
-1 pt for each Stat D
NOTE: Player Id will not be repeated. In case of a tie for fantasy points between two or more players, please sort Player Ids in descending order also. For example, output should be similar to this if two or more players (1234, 100 and 202 here) are tied on 12 fantasy points:
1234,12
202,12
100,12
Sample testcases can be downloaded here, which contains sample input and correct output data.
=== 3. TOP 10 FANTASY PLAYERS ===
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
// ============================================================================ // Author: K Perkins // Date: Nov 26, 2013 // Updated: Jun 17, 2014 // Taken From: http://programmingnotes.org/ // File: Top-10-Fantasy-Players.cpp // Description: Demonstrates a solution to the Yahoo! Sports programming // interview test question titled "Top 10 Fantasy Players." (2 of 3) // ============================================================================ #include <iostream> #include <vector> #include <string> #include <algorithm> #include <cctype> using namespace std; struct Players { int id; int a; int b; int c; int d; int score; }; bool score_cmp(const Players& a, const Players& b); bool id_cmp(const Players& a, const Players& b); void print(vector<Players> stats); int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ Players access; string line; vector<Players> stats; char c = ','; // read a characters until there is no more data while(cin >> c) { // place the character back into the cin buffer cin.unget(); // if the read char was a number, parse the line, else print if(isdigit(c)) { cin >> access.id >> c >> access.a >> c >> access.b >> c >> access.c >> c >> access.d; access.score = (access.a * 2) + (access.b * 1) + (access.c * 6) + (access.d * -1); stats.push_back(access); } else { cin >> line; if(line == "PRINT") { print(stats); } } } return 0; }// end of main bool score_cmp(const Players& a, const Players& b) { return a.score > b.score; }// end of score_cmp bool id_cmp(const Players& a, const Players& b) { return a.score == b.score && a.id > b.id; }// end of id_cmp void print(vector<Players> stats) { // sort the highest scores on top std::sort(stats.begin(), stats.end(), score_cmp); // check if 2 id's have the same score, if they do, place // the higher id above the lower id std::sort(stats.begin(), stats.end(), id_cmp); for(unsigned x = 0; x < stats.size() && x < 10; ++x) { cout << stats[x].id << "," << stats[x].score << endl; } }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
Sample testcases can be downloaded here, which contains sample input and correct output data.
You can read input into the program from a text file using I/O redirection from the Windows command prompt (CMD). Click here if you dont know how to do this.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Once compiled, you should get this as your output:
Sample Input (input003.txt):
1025,4746,38,406,9
2485,102,0,15,1
3115,1225,6,119,8
3116,144,0,12,0
3976,0,0,1,0
4256,5456,39,480,18
4269,10,0,1,0
4319,17,0,1,0
4416,3029,17,293,17
4541,4337,31,385,15
Sample Output:
1025,11957
3115,3162
2485,293
4256,13813
1025,11957
4541,11000
4416,7816
3115,3162
3116,360
2485,293
4319,40
4269,26
3976,6
C++ || Yahoo! Sports – Player IDs Extractor – Programming Interview Test (Question 1 of 3)
The following is sample code which demonstrates a solution to the Yahoo! Sports programming interview question titled “Player-IDs-extractor.”
The test was taken on November 2013 for educational purposes only. Though I completed the test, at the time I had no intention of actually applying for a position, which was for the Junior Backend Test Engineer.
Given two hours, the task was to solve a total of 3 programming questions. The following is question 1 of 3. Questions 2 and 3 can be found here and here.
=== 1. INSTRUCTIONS BEFORE TAKING TEST ===
1. Why take this test? If you get all 3 questions correct you are guaranteed an interview with a Sports Engineer.
2. This is a programming test. Before you begin, Please test your environment with a sample test here.
3. Please make sure you have time free before taking the test. The test usually takes 1 to 2 hours.
4. You are free to choose any language from the list and code.
5. You're expected to write the full code. All inputs are from STDIN and output to STDOUT. If you're using Java, use the classname as 'Solution'.
6. To understand more about the environment, time limits, etc. you can read the FAQ here.
7. You can print to console to debug your code using the appropriate print command for each language (Eg: cout for C++, printf for C, etc.).
=== 2. THE PROBLEM STATEMENT ===
Question 1 / 3 (Player IDs extractor)
Given a list of NBA play-by-play data, please extract the player ids and print them in order of appearance separated by commas.
Each line in the test case represents an NBA play and each line of the output should display the players involved in that play.
Sample Input:
[nba.p.1234] shoots the ball. Assist by [nba.p.4533]
[nba.p.4567] offensive rebound
Sample Output:
nba.p.1234,nba.p.4533
nba.p.4567
Sample testcases can be downloaded here, which contains sample input and correct output data.
=== 3. PLAYER ID’S EXTRACTOR ===
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
// ============================================================================ // Author: K Perkins // Date: Nov 26, 2013 // Updated: Jun 17, 2014 // Taken From: http://programmingnotes.org/ // File: Player-IDs-extractor.cpp // Description: Demonstrates a solution to the Yahoo! Sports programming // interview test question titled "Player-IDs-extractor." (1 of 3) // ============================================================================ #include <iostream> #include <string> #include <vector> using namespace std; vector<string> player_extractor(const string& input); int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ string input = ""; vector<string> names; // get the whole line from the input buffer while(getline(cin, input)) { names = player_extractor(input); for(unsigned x = 0; x < names.size(); ++x) { cout << names[x]; if((names.size() > 1) && (x < names.size()-1)) { cout << ","; } } cout << "n"; } return 0; }// end of main vector<string> player_extractor(const string& input) { vector<string> names; // loop thru the line for(unsigned x = 0; x < input.length(); ++x) { if(input.length() > 1) { // if this is what we are looking for, parse it if(input[x] == '[' && input[x + 1] == 'n') { string output = ""; unsigned length = 0; // get the player id name for(length = x + 1; length < input.length() && input[length] != ']'; ++length) { output += input[length]; } // save the player id to the vectorr names.push_back(output); x = length + 1; }// end if }// end if }// end for return names; }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
Sample testcases can be downloaded here, which contains sample input and correct output data.
You can read input into the program from a text file using I/O redirection from the Windows command prompt (CMD). Click here if you dont know how to do this.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Once compiled, you should get this as your output:
Sample Input:
[nba.p.1234] shoots the ball. Assist by [nba.p.4533]
[nba.p.4567] offensive rebound
Sample Output:
nba.p.1234,nba.p.4533
nba.p.4567
C++ || Snippet – Custom “strtok” Function To Split A String Into Tokens Using Multiple Delimiters
The following is a custom “strtok” function which splits a string into individual tokens according to delimiters. So for example, if you had an string full of punctuation characters and you wanted to remove them, the following function can do that for you.
This function works by accepting an std::string containing the text to be broken up into smaller std::strings (tokens), aswell as another std::string that contains the delimiter characters. After the parse is complete, it returns a vector object containing all the found tokens in the string.
The code demonstrated on this page is different from the cstring strtok function in that this implementation works for C++ only.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
// ============================================================================ // Author: Kenneth Perkins // Date: Jun 13, 2014 // Taken From: http://programmingnotes.org/ // File: Strtok.cpp // Description: Demonstrates the use of a custom "strtok" function which // splits a string into individual tokens according to delimiters. // ============================================================================ #include <iostream> #include <string> #include <vector> using namespace std; // function prototype vector<string> str_tok(const string& str, const string& delimiters); int main() { // declare variables string str = "- This, is. a sample string? 3!30$' 19: 68/, LF+, 1, 1"; string delimeters = " ,.-':;?()+*/\%$#!\"@^&"; vector<string> tokens; cout << "The original string: " << str << endl; // split the string into tokens according to the delimeter tokens = str_tok(str, delimeters); cout << "\nThe tokens: \n"; for(unsigned x = 0; x < tokens.size(); ++x) { cout << tokens[x] << endl; } return 0; }// end of main /** * FUNCTION: str_tok * USE: Splits a string into individual tokens and saves them into a vector. * @param str: A std::string to be broken up into smaller std::strings (tokens). * @param delimiter: A std::string containing the delimiter characters. * @return: A vector containing all the found tokens in the string. */ vector<string> str_tok(const string& str, const string& delimiters) { std::size_t prev = 0; std::size_t currentPos = 0; vector<string> tokens; // loop thru string until we reach the end while((currentPos = str.find_first_of(delimiters, prev)) != string::npos) { if(currentPos > prev) { tokens.push_back(str.substr(prev, currentPos - prev)); } prev = currentPos + 1; } // if characters are remaining, save to vector if(prev < str.length()) { tokens.push_back(str.substr(prev, string::npos)); } return tokens; }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Once compiled, you should get this as your output
The original string: - This, is. a sample string? 3!30$' 19: 68/, LF+, 1, 1
The tokens:
This
is
a
sample
string
3
30
19
68
LF
1
1