Spotify Job Site Problems In this document you’ll get a sneak peek at the problems from Spotify’s job site. Instructions: • To submit a solution to one of the problems, send an e-mail to [email protected]
with your source code files as attachments and the problem id or problem name as subject. For instance, to submit to the example problem “A Different Problem”, the e-mail should have subject “different” or “A Different Problem”. Within minutes you will get a reply indicating whether your source code solved the problem, and if not, an indication of what was wrong with it. • Your programs can be written in either C, C++, Java, or Python. • Your programs should read data from standard input (stdin) and write to standard ouput (stdout). Happy coding!
Problems A Different Problem Best Before Zipf’s Songs Ticket Lottery Bilateral Projects
A Different Problem Problem ID: different Write a program that computes the difference between positive integers.
Input The input contains several pairs of integers. Each integer is between 0 and 263 − 1. The input is terminated by end of file.
Output For each pair of integers in the input, output one line, containing the absolute value of their difference. Sample input 1 10 12 1871293781758123 72784 1 9223372036854775807
Sample output 1 2 1871293781685339 9223372036854775806
Spotify Job Site Problem: A Different Problem
Best Before Problem ID: bestbefore Just about any product found in a grocery store has a “best before” date printed on it. Unfortunately, the format for these dates can vary quite a bit: what is it supposed to mean that the bread you bought yesterday is “best before 12/11/10”? It could mean that the bread expires on November 10, 2012 (now that’s a suspiciously durable bread and probably it is not healthy for you for other reasons!), or it could mean that the bread expired on December 11, 2010 (d’oh!), or a variety of other options. To be safe, the truly paranoid (or healthily skeptic, depending on who you ask) person would assume that it means “best before November 12, 2010”, since that is the earliest possible date. On the other hand, Picture by Henry M¨uhlpfordt (Own work) via Wikimedia Commons seeing “31/5/2012” even the truly paranoid person knows that this must mean May 31, 2012 since that is the only possible real date using these three numbers. Given a possibly ambiguous date “A/B/C”, where A, B, C are integers between 0 and 2999, output the earliest possible legal date between Jan 1, 2000 and Dec 31, 2999 (inclusive) using them as day, month and year (but not necessarily in that order). Recall that a year is a leap year (has 366 days) if the year is divisible by 4, unless it is divisible also by 100 but not by 400 (so 2000 is a leap year, 2100 is not a leap year, and 2012 is a leap year).
Input The input file consists of a single line containing three integers separated by “/”. There are no extra spaces around the “/”. Years may be truncated to two digits and may in that case also omit the leading 0 (if there is one), so 2000 could be given as “2000”, “00” or “0” (but not as an empty string). Months and days may be zero-padded. You may assume that the year, when given with four digits, is between 2000 and 2999. At most one of the integers has four digits, and the others have one or two digits.
Output Output a single line giving the earliest legal date possible given the above constraints. The output should be formatted as year-month-day, where year has four digits, and month and day have two digits each (zero padding), for example “2011-07-15”. If there is no legal date (subject to the above constraints) then output a single line with the original string followed by the words “is illegal”. Sample input 1 02/4/67
Sample output 1 2067-02-04
Sample input 2 31/9/73
Sample output 2 31/9/73 is illegal
Spotify Job Site Problem: Best Before
Zipf’s Songs Problem ID: zipfsong Your slightly pointy-bearded boss has assigned you to write software to find the best songs from different music albums. And the software should be finished in an hour. But don’t panic, you don’t have to solve the problem of writing an AI with good taste. At your disposal is the impeccable taste of a vast horde of long-tailed monkeys. Well, at least almost. The monkeys are not very communicative (or rather, you’re not sure which song “Ook!” is supposed to refer to) so you can’t ask them which songs are the best. What you can do however is to look at which songs the monkeys have listened to and use this information to deduce which songs are the best. Photo by suneko on Flickr At first, you figure that the most listened to songs must be the best songs. However, you quickly realize that this approach is flawed. Even if all songs of the album are equally good, the early songs are more likely to be listened to more often than the later ones, because monkeys will tend to start listening to the first song, listen for a few songs and then, when their fickle ears start craving something else, stop listening. Instead, if all songs are equal, you expect that their play frequencies should follow Zipf’s Law. Zipf’s Law is an empirical law originally formulated about word frequencies in natural languages, but it has been observed that many natural phenomena, such as population sizes and incomes, approximately follow the same law. It predicts that the relative frequency of the i’th most common object (in this case, a song) should be proportional to 1/i. To illustrate this in our setting, suppose we have an album where all songs are equally good. Then by Zipf’s Law, you expect that the first song is listened to twice as often as the second song, and more generally that the first song is listened to i times as often as the i’th song. When some songs are better than others, those will be listened to more often than predicted by Zipf’s Law, and those are the songs your program should select as the good songs. Specifically, suppose that song i has been played fi times but that Zipf’s Law predicts that it would have been played zi times. Then you define the quality of song i to be qi = fi /zi . Your software should select the songs with the highest values of qi .
Input The first line of input contains two integers n and m (1 ≤ n ≤ 50 000, 1 ≤ m ≤ n), the number of songs on the album, and the number of songs to select. Then follow n lines. The i’th of these lines contains an integer fi and string si , where 0 ≤ fi ≤ 1012 is the number of times the i’th song was listened to, and si is the name of the song. Each song name is at most 30 characters long and consists only of the characters ‘a’-‘z’, ‘0’-‘9’, and underscore (‘ ’).
Output Output a list of the m songs with the highest quality qi , in decreasing order of quality. If two songs have the same quality, give precedence to the one appearing first on the album (presumably there was a reason for the producers to put that song before the other). Sample input 1 4 2 30 one 30 two 15 three 25 four
Spotify Job Site Problem: Zipf’s Songs
Sample output 1 four two
Sample input 2 15 3 197812 re_hash 78906 5_4 189518 tomorrow_comes_today 39453 new_genious 210492 clint_eastwood 26302 man_research 22544 punk 19727 sound_check 17535 double_bass 18782 rock_the_house 198189 19_2000 13151 latin_simone 12139 starshine 11272 slow_country 10521 m1_a1
Spotify Job Site Problem: Zipf’s Songs
Sample output 2 19_2000 clint_eastwood tomorrow_comes_today
Ticket Lottery Problem ID: lottery You and your friends are in New York and are planning to go see a broadway musical. Unfortunately, New York being New York, the tickets are just a tiny bit expensive. But one of the shows has a ticket lottery each night where impecunious people such as yourself have a chance to win the right to buy slightly less expensive tickets to good seats. The lottery operates as follows. First, everyone interested enters the lottery. Then, n lucky winners are drawn, and each of these is offered to buy up to t tickets. Given the number of people p in your group (all of which entered the lottery) and the total number of people m that entered the lottery, what is the probability that you will be able to get tickets for your entire group? Assume that the n lucky winners are chosen uniformly at random from the m people that entered the lottery, and that each person can win at most once.
Input The input consists of a single line containing four integers:
Picture in Public Domain via Wikimedia Commons
• 1 ≤ m ≤ 1000: the total number of people who entered the lottery. • 1 ≤ n ≤ m: the total number of winners drawn. • 1 ≤ t ≤ 100: the number of tickets each winner is allowed to buy. • 1 ≤ p ≤ m: the number of people in your group.
Output Output a single line containing the probability that your entire group can get tickets to the show. This probability should be given with an absolute error of at most 10−9 . Sample input 1 100 10 2 1
Sample output 1 0.1
Sample input 2 100 10 2 2
Sample output 2 0.1909090909
Sample input 3 10 10 5 1
Sample output 3 1.0000000000
Spotify Job Site Problem: Ticket Lottery
Bilateral Projects Problem ID: bilateral A friend of yours works at an undisclosed company in the music streaming industry, and needs your help. The company has offices in Stockholm and London, and collaboration between the two offices is extensive. The situation is that each of the many but small projects are handled by a two-person team with a member in each city. While emails, faxes, and phones are wonderful, and work well within each team, the CEO wants a briefing every year on the projects. For this purpose the CEO invites representatives from the projects to Barbados for a week of beach fun presentations of all the projects. However, money is tight and a new policy has been created: the CEO wants at least one person from each project, and additionally, she wants to invite as few people as possible. This is where Picture by user Acdx via Wikimedia Commons you come in. In order to help your friend get a ticket to Barbados, you are to write a program that, given all the two-person teams, computes the smallest number of people that must be invited in order to get at least one person from each project, as well as a list of people to invite. If possible (subject to the set of people being smallest possible), the list of invitees should include your friend.
Input The first line of input contains an integer 1 ≤ m ≤ 10 000, the number of teams. The following m lines each contain two integers, i, j separated by a space, being the employee IDs of the two employees in that team (the first one is from Stockholm and the second one is from London). Stockholm employees have IDs in the range 1000 to 1999 and London employees have IDs in the range 2000 to 2999. An employee can be a member of several teams, but there cannot be several teams consisting of the same pair of employees. Your friend has ID 1009.
Output Output first a single line with an integer k indicating the smallest number of employees that must be invited to meet the requirements above. Then output k lines giving the IDs of employees to invite. If possible (subject to k being smallest possible), the list should contain your friend. If there are several solutions subject to these constraints, anyone is acceptable. Sample input 1 2 1009 2011 1017 2011
Sample output 1 1 2011
Sample input 2 4 1009 2000 1009 2001 1002 2002 1003 2002
Sample output 2 2 2002 1009
Spotify Job Site Problem: Bilateral Projects