Tech/HowTo/Python Interview Challenges

From lathama
< Tech‎ | HowTo
Jump to navigation Jump to search

Introduction

Challenges

Hosts File Parser

In a given hosts file parse out the host and address as a list indexed by the hostname
127.0.0.1       localhost
127.0.1.1       lappy7.lathama.net      lappy7
127.0.0.2       www.facebook.com        facebook.com # block facebook
192.168.15.235  SGM-STL-OBM-001.LATHAMA.NET     SGM-STL-OBM-001

 # comment line that starts with whitespace
# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Basic Web Service

Write a service to return the current time

Steps to Palindrome Number

Write a function that takes an integer which is checked to be a palindrome (same backwards and forwards) if not adds the reverse and then checks. The output is the number of steps it took to get to the palindrome.

Solutions

Hosts File Parser

#!/usr/bin/env python3

import re


def parse_hosts(hosts):
    """
    Check a hosts file and import the non-comments
    Ignore comments, blank lines etc
    We have to assume it is a valid hosts file
    """
    hostlist = []
    return hostlist

if __name__ == '__main__':
    print(parse_hosts('/etc/hosts'))

Steps to Palindrome Number

#!/usr/bin/env python3

def palindrome_check(n):
    return n == int(str(n)[::-1])

def palindrome_steps(n):
    steps = 0
    while not palindrome_check(n):
        n = n + int(str(n)[::-1])
        steps = steps + 1
    return steps

def test_palindrome_steps(number, steps):
    if palindrome_steps(number) == steps:
        print("\n\tCorrect\n")
    else:
        print("\n\tIncorrect\n")
        print("Input %d returned output %d" % (number, palindrome_steps(number)))

if __name__ == '__main__':
    test_palindrome_steps(87, 4)


Questions

Found some questions that I like at https://groups.google.com/forum/#!topic/comp.lang.python/rhW_rIYY5HM/discussion%5B1-25%5D

Basic Python:
=============
- do they know a tuple/list/dict when they see it?

- when to use list vs. tuple vs. dict. vs. set

- can they use list comprehensions (and know when not to
   abuse them? :)

- can they use tuple unpacking for assignment?

- string building...do they use "+=" or do they build a list
   and use .join() to recombine them efficiently

- truth-value testing questions and observations (do they
   write "if x == True" or do they just write "if x")

- basic file-processing (iterating over a file's lines)

- basic understanding of exception handling

Broader Basic Python:
=====================
- questions about the standard library ("do you know if
   there's a standard library for doing X?", or "in which
   library would you find [common functionality Y]?")  Most
   of these are related to the more common libraries such as
   os/os.path/sys/re/itertools

- questions about iterators/generators

- questions about map/reduce/sum/etc family of functions

- questions about "special" methods (__<foo>__)

More Advanced Python:
=====================
- can they manipulate functions as first-class objects
   (Python makes it easy, but do they know how)

- more detailed questions about the std. libraries (such as
   datetime/email/csv/zipfile/networking/optparse/unittest)

- questions about testing (unittests/doctests)

- questions about docstrings vs. comments, and the "Why" of
   them

- more detailed questions about regular expressions

- questions about mutability

- keyword/list parameters and unpacked kwd args

- questions about popular 3rd-party toolkits (BeautifulSoup,
   pyparsing...mostly if they know about them and when to use
   them, not so much about implementation details)

- questions about monkey-patching

- questions about PDB

- questions about properties vs. getters/setters

- questions about classmethods

- questions about scope/name-resolution

- use of lambda

Python History:
===============
- decorators added in which version?

- "batteries included" SQL-capible DB in which version?

- the difference between "class Foo" and "class Foo(object)"

- questions from "import this" about pythonic code

Python Resources:
=================
- what do they know about various Python web frameworks
   (knowing a few names is usually good enough, though
   knowledge about the frameworks is a nice plus) such as
   Django, TurboGears, Zope, etc.

- what do they know about various Python GUI frameworks and
   the pros/cons of them (tkinter, wx, pykde, etc)

- where do they go with Python related questions (c.l.p,
   google, google-groups, etc)

Other Process-releated things:
==============================
- do they use revision control
   (RCS/CVS/Subversion/Mercurial/Git...anything but VSS) and
   know how to use it well

- do they write automated tests for their code

Touchy-feely things:
====================
- tabs vs. spaces, and their reasoning

- reason for choosing Python

- choice of editor/IDE