Steve Yegge’s Phone-Screen Coding Exercises
June 30, 2009
Steve Yegge is a programmer and popular blogger. One of his blog entries proposes these seven phone-screen coding exercises:
- Write a function to reverse a string.
- Write a function to compute the Nth fibonacci number.
- Print out the grade-school multiplication table up to 12 x 12.
- Write a function that sums up integers from a text file, one per line.
- Write a function to print the odd numbers from 1 to 99.
- Find the largest int value in an int array.
- Format an RGB value (three 1-byte numbers) as a 6-digit hexadecimal string.
Your task is to write solutions to stevey’s phone-screen exercises. When you are finished, you are welcome to read a suggested solution, or to post your solution or discuss the exercise in the comments below.
[…] Praxis – Steve Yegge’s Phone-Screen Coding Exercises By Remco Niemeijer Today’s Programming Praxis problem is an easy one. In 2005, Steve Yegge posted an article about […]
My Haskell solution (see http://bonsaicode.wordpress.com/2009/06/30/programming-praxis-steve-yegge%E2%80%99s-phone-screen-coding-exercises/ for a version with comments):
import Text.Printf import Data.Word reverse' :: String -> String reverse' = foldr (:) [] fib :: (Num a) => Int -> a fib n = fibs !! n where fibs = 0 : 1 : zipWith (+) fibs (tail fibs) timesTable :: IO () timesTable = mapM_ putStrLn [concat [printf "%4d" (a * b) | a <- [1..12]] | b <- [1..12 :: Int]] sumFile :: FilePath -> IO () sumFile path = print . sum . map read . lines =<< readFile path printOdds :: IO () printOdds = print $ filter odd [1..99] largest :: [Int] -> Int largest = foldl max 0 toHex :: Word8 -> Word8 -> Word8 -> String toHex = printf "%02x%02x%02x"C# Solution
I couldn’t resist writing smartass responses for questions three and five to just return fixed strings. I also wrote more general answers that I thought were more in keeping with the spirit of the exercise.
The basic recursive implementation for Fibonacci numbers is a classic, but is terribly slow. It’s the answer I’d expect out of most interview candidates as a first pass since it is taught so regularly as an example of recursion. However, they’d be in trouble if they couldn’t quickly identify the problems with such an implementation. I wrote out a couple of alternatives that are a bit more efficient.
Thanks to nonowarn for catching a bug:
The correct code for exercise 1 should be
I think you’ve got them all, Remco. My Haskell solutions are similar. Here is one simpler solution to #5:
printOdds = print [1,3..99]
There’s one more bug in Remco’s code, though: largest assumes positive integers, but it should read:
largest = foldl1 max
Yeah, I noticed that one too, but didn’t get round to fixing it. However, if you treat Steve Yegge’s solution as the spec then the correct implementation is actually
largest = foldl max minBound
def reverse(str) # 1. Write a function to reverse a string. len = str.length - 1 len.downto(0).collect {|n| str[n].chr}.to_s end def nth_fib(n) # 2. Write a function to compute the Nth fibonacci number. # NB. assumes fibonacci numbers are zero-indexed. x,y = 0,1 n.times {x,y = y,x+y} y end def multi_table # 3. Print out the grade-school multiplication table up to 12 x 12. size = 12 1.upto(size).each do |x| 1.upto(size).each do |y| printf "%4d", x*y end printf "\n" end end def sum_ints_from_file(filename) # 4. Write a function that sums up integers from a text file, one per line. sum = 0 File.open(filename).each {|line| sum += line.to_i} sum end def print_odd_numbers # 5. Write a function to print the odd numbers from 1 to 99. (1..99).step(2).each {|n| puts n} ; nil end def find_largest_value(arr) # 6. Find the largest int value in an int array. arr.inject {|x,y| x > y ? x : y} end def format_RGB(r,g,b) # 7. Format an RGB value (three 1-byte numbers) as a 6-digit hexadecimal string. format "%02X%02X%02X", r,g,b end[…] for the exercise, I picked Steve Yegge’s Phone-Screen Coding Exercises, figuring seven different small problems might cover slightly more ground than one bigger […]
I’m never sure how much of a language’s built-in functionality we’re supposed to use for a question.
Python solution
Common Lisp solution
For the RGB => Hex conversion you can always take advantage API knowledge (such as string formatting features), but its not as fun!
public static string ConvertToHex(byte r, byte g, byte b) { char[] buffer = new char[6]; int rgb = (r << 16) | (g << 8) | b; for (int i = 0; i < 6; i++) { int val = rgb & 15; if (val <= 9) buffer [5 - i] = (char)('0' + val); else buffer [5 - i] = (char)('A' + (val - 10)); rgb >>= 4; } return new string(buffer); }