C++ || Yahoo! Sports – Current Batting Order – Programming Interview Test (Question 3 of 3)

Print Friendly

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 ===


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

Leave a Reply