## 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:

1. Write a function to reverse a string.
2. Write a function to compute the Nth fibonacci number.
3. Print out the grade-school multiplication table up to 12 x 12.
4. Write a function that sums up integers from a text file, one per line.
5. Write a function to print the odd numbers from 1 to 99.
6. Find the largest int value in an int array.
7. 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.

Pages: 1 2

### 9 Responses to “Steve Yegge’s Phone-Screen Coding Exercises”

1. [...] 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 [...]

2. Remco Niemeijer said

```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"
```
3. Scott Pedersen said

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.

4. Remco Niemeijer said

Thanks to nonowarn for catching a bug:

The correct code for exercise 1 should be

```reverse' :: String -> String
reverse' = foldl (flip (:)) []
```
5. liesen said

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

6. Remco Niemeijer said

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

7. Connochaetes said
```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

```
8. [...] 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 [...]

9. Graham said

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