The OA was for Software Engineer Internship 2024 and below is the second question from it.
As a long time live-music fan you have kept a bucket list of concerts you want to see. You have the means to buy as many concert tickets as you want, however due to scalpers and resellers, you struggle to actually score online tickets for a reasonable price. Thankfully, a friend has alerted you to a service called BuyFirst, which streams real-time data about freshly available concert tickets before anybody else. BuyFirst provides a binary data stream for fast access to their data.
You have decided to use your programming skills to successfully get concert tickets. Your bucket list has concerts for the artists you want to see at various locations. As an avid music fan you are happy to see the same artist in multiple locations since they will play different sets. You have also decided that you are willing to pay up to a certain ticket price, but no more. You also want a minimum seating category since you don't want to be stuck in the noebleeds. Finally, concerts are only worth going to with friends, so you want to buy a certain number of available seats per concert.
BuyFirst will stream AvailableTicket messages on their data feed. BuyFirst provides an API to submit an Order to buy a ticket. Your objective is to write a program to listen on available tickets, and submit buy-orders for ticket that match your criteria as soon as they become available.
Assume that:
Format of AvailableTicket messages
Messages from BuyFirst are not received as a whole, but packet-by-packet:
message_id (4 bytes) artist_id (4 bytes)message_id (4 bytes) location_id (4 bytes)message_id (4 bytes) ticket_price (4 bytes)message_id (4 bytes) category (4 bytes)message_id (4 bytes) available_seats (4 bytes)Note the following:
message_id is the same for all packets of the same messagemessage_id is unique for each message and will not be reusedmessage_id the first packet contains the artist_id, the second packet contains the location_id, etc.)Format of Order messages
When ordering tickets, the message for doing so is also sent packet-by-packet:
original_message_id (4 bytes)number_of_seats (4 bytes)These packets are sent contigiously. They cannot be interleaved.
Your task is to implement the following functions:
OnNewRequirement(artist, location, ticket_price, category, available_seats):
This is called when a requirement is added and its parameters correspond to the fields of the AvailableTicket message.
Returns:
ProcessData(messageId, data):
This is called for every incoming packet of AvailableTicket message, and has the following parameters:
messageId - message identifier; 0 means "no data"data - data payload corresponding to the ticket for messageId. It will indicate one of the artist_id, location_id, ticket_price, category, available_seats when the messageId is not 0Returns:
data - uint_32_t type of the Order data packet to send, or 0 if there is nothing to sendThe following applies to match a requirement against an AvailableTicket:
artist_id is equal to the required artist_idlocation_id is equal to the required location_idticket_price is lower than or equal to the required ticket_pricecategory is higher than or equal to the required categoryavailable_seats is higher than or equal to the required available_seatsWhen sending an order, note that:
AvailableTicket messagemessage_id of the AvailableTicket message you want to buy in the original_message_id fieldProcessData(): if a valid/nonzero original_message_id packet has been returned, then the number_of_seats packet must be returned from the next immediate call to complete the orderAvailableTicketAvailableTicket messages, since the buying opportunity is assumed to be goneAvailableTicket message have arrived0 <= message_id < 2^320 <= artist_id < 2^320 <= location_id < 2^320 <= ticket_price < 2^320 <= category < 2^321 <= available_seats < 2^32Input to the program is specified using a simple text format. The format and details of parsing are not relevant to answering the question. Custom input can be used to help with development and debugging.
The first line specifies the number of REQUIREMENT and DATA as follows:
REQUIREMENT [artist_id] [location_id] [ticket_price] [category] [available_seats]
OnNewRequirement()DATA [message_id] [data]
ProcessData()message_id=0 indicates that there is no valid data16
REQUIREMENT 1 1 100 1 2
DATA 1 1
DATA 1 1
DATA 1 110
DATA 1 3
DATA 0 0
DATA 2 1
DATA 2 1
DATA 2 90
DATA 2 3
DATA 2 3
DATA 0 0
DATA 0 0
DATA 0 0
DATA 0 00
0
0
0
0
0
0
0
0
0
2
2
0
0
0The message with id 1 does not fulfill the requirement because the price of 110 is higher than the required 100. The message with id 2 does fulfill the requirement. The requirements is fulfilled once the number of available seats is received, so an order message for original message id 2 and 2 seats (as per requirement) is sent. At all other times, no message is being send (0).