Tag Archives: Player IDs Extractor
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