Inverse Fizzbuzz
jasq.org
jasq.org
Matching the the sequence of fizz/buzz/fizbuzz disregarding numbers is a repetitive case.
Start with
3 Fizz
5 Buzz
6 Fizz
9 Fizz
10 Buzz
12 Fizz
15 FizzBuzz
18 Fizz
20 Buzz
21 Fizz
24 Fizz
25 Buzz
27 Fizz
30 FizzBuzz
[repeats]
Just split the input list as a prefix of a few, and then a repeat. The goal of the shortest total distance between them is constant. You just need to measure the number of ints between each of the 8 repeating distances.For example:
3 -> Fizz
+2 -> Buzz
+1 -> Fizz
+3 -> Fizz
+1 -> Buzz
+2 -> Fizz
+3 -> FizzBuzz
+3 -> Fizz
+2 -> Buzz
+1 -> Fizz
+3 -> Fizz
+1 -> Buzz
+2 -> Fizz
+3 -> FizzBuzz
So you would match against the list [Fizz, Buzz, Fizz, Fizz, Buzz, Fizz, FizzBuzz].On the other hand, checking to ensure that the input represents a valid sequence is going to be O(n) in the size of the input no matter what you do...
Now, since 15 is divisible by 3, it's easy to see that x = 0 mod 3 iff x = {0, 3, 6, 12} mod 15, and similarly x = 0 mod 5 iff x = {0, 5, 10} mod 15. So you can see now that you can determine the output for a given index based on the remainder after dividing by 15, so the sequence must repeat every 15 elements.
Your program might receive bad input, and it's your responsibility to output an error code if so.
For example the first question, "How do you map the list on the left to the list on the right?" Huh? What do you mean by "map"? Do you want a data structure to link the two? Are the list infinite? Or just upto 100? Do you want to derive the algorithm to produce the next number? Or do you want a function that given the left list produce the right list? Or do you want a function that given a number from the left list, produce the corresponding item on the right. On and on.
This can backfire, and requires a bit of fluidity in the interviewer's part -- no cargo cult questions, just open problem solving. In a really good case, me being open to what the interviewee was saying about ambiguity led to a fascinating attempt to solve a different problem than I had originally structured, but we found out we work well together and solved a fun problem. At the same time he showed me his skills, and I showed him my skills. Result: both of us decided working together would be beneficial -- the best possible outcome of an interview (although just as good would be mutually deciding we wouldn't work well together and amicably parting ways).
I'm sure you've met arsonist developers that light buildings on fire just to see if they can escape them.
Also, very often, it's not software that does not scale, it's people working on the software. Clearly, there are relatively few developers in the marketplace who can master the most advanced software development techniques. In practice, "clever" code will be misunderstood sooner or later, its complexity will increase as "lesser" developers patch it down to a level where they can understand it, expand it and correct it. And so you end up with large balls of mud, initially clever code turned into tremendous sources of complexity.
If you look at the pillars of Internet, HTTP, FTP, telnet, IP, TCP, POP, SMTP, etc. There is 1 common factor between them: They are dead simple solutions to specific problems. And that is why they stood the test of time.
<EDIT>
Yes, here's a constant solution: http://codepad.org/Id9eu0Ax
For any sequence containing fizzbuzz, match up the fizzbuzz with 15 and you're done. Any sequence of length >=7, fizzbuzz will be present. Thus, we only need to think about sequences length <7 that don't contain fizzbuzz. Solving this by brute force is constant time, since you only have finite possible inputs.
Are you sure? I don't think the challenge said the sequence could not be gappy.
For your example a shorter one would be 9,10,15.
It's O(1) to find a candidate solution, but of course it will still be O(N) to verify that the solution really works.
raw_input = ["Fizz","Buzz","Fizz","Fizz","Buzz","Fizz","FizzBuzz","Buzz","Buzz"]
Making the solution "functional" was by far the most time-consuming part of the exercise.
"Very funny (and an interesting post). Go ahead! also definitely let me know when it's "public" i'd like to tweet it out" - Dan Shipper.
The other Stuff Smith version of "Ise a Muggin" has resulted in a question I ask older Railroad workers (and fans): What exactly did a "Black Cap" or a "Blue Cap" do? (It also speaks of Red Caps, but we still have that profession here in the USA.)
Anybody knows other articles of this kind ?
Edit: you don't even need to specify the bounds, just run the forward algorithm for a large enough range (length of input list x 15 will be more than sufficient).
Which world do you speak of? Most of the jobs I'm aware of (for example, java, .net, php, etc.) are still a mix of OOP and procedural.