• About
  • Bookmarks
Robert-Reinder Nederhoed Internet, Bitcoin en koken (weblog in ruste)

Five essential phone-screen questions

January 4, 2007 18:46 / Leave a Comment / Robert-Reinder Nederhoed

For just five interview questions, it surely ended up as a long read. Nevertheless worth the read:

The Five Essential Phone-Screen Questions — Steve Yegge

I just might translate some of his examples to Python:

Example 1: Write a function to reverse a string

def reverse(text):
....result = list(text)
....return ''.join(result[::-1]) 

or, just

def reverse(text):
....return text[::-1]

Example 2: Write function to compute Nth fibonacci number

def fib(n):
...."""Calculate fibonacci value for given n
....Doctests:
....>>> [fib(i) for i in xrange(0, 10)
....[0, 1, 1, 2, 3, 5, 8, 12, 21, 34, 55]
...."""
....if n 

Example 3: Print out the grade-school multiplication table up to 12x12

def multiplication_table(n):
...."""Generate an n*n multiplication table
....>>> len(multiplication_table(10))
....10
....>>> len(multiplication_table(5)[0])
....5
...."""
....# Create matrix
....table = [[x*y for x in xrange(1, n+1)] for y in xrange(1, n+1)]
....return table

def print_matrix(matrix):
...."""Expects a list of lists """
....for row in matrix:
....|...print "%4d" * len(row) % tuple(row)

if __name__ == "__main__":
...."""Run actual program code """
....print_matrix(multiplication_table(12))

Example 4: Write a function that sums up integers from a text file, one int per line

def sum_file(filename):
...."""TODO: catch exceptions """
....fp = file(filename)
....try:
....|...numbers = [int(line.strip()) for line in fp]
....finally:
....|...fp.close()
....return sum(numbers)

Example 5: Write function to print the odd numbers from 1 to 99

def print_odds():
....for i in xrange(0, 100, 2):
....|...print i 

Example 6: Find the largest int value in an int array

def largest(sequence):
...."""Find the largest occurrence in a sequence ('array')
....>>> largest([0])
....0
....>>> largest(range(100))
....99
....>>> largest([1, 0, -1])
....1
...."""
....return max(sequence)

Example 7: Format an RGB value (three 1-byte numbers) as a 6-digit hexadecimal string

def format_rgb(red, green, blue):
...."""Convert integer RGB to hex RGB string
....
....>>> format_rgb(0, 0, 0)
....'000000'
....>>> format_rgb(255, 0, 0)
....'FF0000'
....>>> format_rgb(0, 255, 0)
....'00FF00'
....>>> format_rgb(0, 0, 255)
....'0000FF'
....>>> format_rgb(255, 255, 255)
....'FFFFFF'
...."""
....return "%02X%02X%02X" % (red, green, blue)

Test solutions:

def _test():
...."""Run tests on functions in module """
....import doctest
....doctest.testmod()

Please ignore points and pipes. WordPress can't handle code layout well. Python is white-space sensitive. This is the best I could come up with.

Posted in: Development, Object Oriented Design, Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Post Navigation

← Previous Post
Next Post →

Recent Posts

  • LeWeb 2013: about Bitcoin
  • Rule of acquisition #303
  • Recreational betting with Bitcoins
  • Search activity on Google related to "bitcoin" and its value
  • Bitcoin Statistics to study

Recent Comments

  • ifoods on Ordering food online: a short adventure
  • Evening photoshoot with Margie (and more) « Nederhoed on New camera Canon EOS 1000D
  • R.R. Nederhoed on OmniGraffle equivalent anyone?
  • watziterinhetmais on Where is my mind?
  • warddr on About

Archives

  • June 2013
  • March 2013
  • August 2012
  • March 2012
  • May 2011
  • May 2010
  • March 2010
  • January 2010
  • December 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • January 2009
  • November 2008
  • October 2008
  • July 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005

Categories

  • Bitcoin
  • Books
  • Development
  • Finance
  • Idealism
  • JavaScript
  • Language
  • Linux
  • Mining
  • Music
  • Object Oriented Design
  • Personal
  • Photography
  • Python
  • Quotes
  • Ruby
  • Spending
  • The Hague
  • Uncategorized
  • Web
  • XML
  • Zope

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org
© Copyright 2019 - Robert-Reinder Nederhoed
Infinity Theme by DesignCoral / WordPress