Skip to content

Instantly share code, notes, and snippets.

@luckylittle
Last active September 6, 2022 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save luckylittle/ad3451cfda74d47846c0c3e8983a2291 to your computer and use it in GitHub Desktop.
Save luckylittle/ad3451cfda74d47846c0c3e8983a2291 to your computer and use it in GitHub Desktop.
Preparing for a Python Interview: 10 Things you should know

#1 KNOW HOW TO WRITE CODE ON A WHITEBOARD/PAPER

#2 KNOW BASIC PYTHON CONTROL FLOW: controlflow.py

a)

for i in range(1,11):
  print i

b)

i = 1
while i <= 10:
  print i
  i += 1

c)

a = 10
b = 20
if a < b:
  print "{} is less than {}".format(a, b)
elif a == 20:
  print "{} is equal to {}".format(a, b)
else:
  print "{} is greater than {}".format(a, b)

#3 BE ABLE TO DISCUSS HOW YOU'VE USED PYTHON: filesystem.py

import os, glob
os.chdir("/home/lmaly/Projects/Python")
for file in glob.glob("*.jpg"):
  print(file)

#4 KNOW HOW TO SOLVE COMMON INTERVIEW PROBLEMS: common.py

a) Fizz Buzz

for num in xrange(1,101):
  if num % 5 == 0 and num % 3 == 0:
    print "FizzBuzz"
  elif num % 3 == 0:
    print "Fizz"
  elif num % 5 == 0:
    print "Buzz"
  else:
    print num

b) Fibonacci seq

a, b = 0, 1
for i in xrange(0,10):
 print a
 a, b = b, a + b

#5 KNOW BASIC PYTHON DATA TYPES AND WHEN TO USE THEM: datatypes.py

a) Lists

my_list = [10,20,30,40,50]

b) Tuples

my_tup = (1,2,3,4,5,6,7,8,9,10)

c) Dict

my_dict = {'name': 'Bronx', 'age': '2', 'occupation': "Corey's Dog"}
for key, val in my_dict.iteritems():
  print "My {} is {}".format(key, val)

d) Set

my_set = {10,20,30,40,50,10,20,30,40,50}

#6 KNOW HOW TO USE LIST COMPREHENSIONS: list_comp.py

  my_list = [1,2,3,4,5,6,7,8,9,10]
  squares = [num*num for num in my_list]
  print squares

#7 KNOW HOW TO USE GENERATORS: common.py

a) Fibonacci gen

def fib(num):
  a, b = 0, 1
  for i in xrange(0, num):
    yield "{}: {}".format(i+1, a)
    a, b = b, a + b
for item in fib(10):
  print item

#8 KNOW THE BASICS OF OOP: oop.py

class Person(object):
  def __init__(self, name):
    self.name = name

  def reveal_identity(self):
    print "My name is {}".format(self.name)

class SuperHero(Person):
  def __init__(self, name, hero_name):
    super(SuperHero, self).__init__(name)
    self.hero_name = hero_name

  def reveal_identity(self):
    super(SuperHero, self).reveal_identity()
    print "...And I am {}".format(self.hero_name)

corey = Person('Corey')
corey.revel_identity()

wade = SuperHero('Wade Wilson', Deadpool')
wade.reveal_identity()

#9 KNOW BASIC SORTING ALGORITHMS

a) Bubble

def bubble_sort(items):
      """ Implementation of bubble sort """
      for i in range(len(items)):
              for j in range(len(items)-1-i):
                      if items[j] > items[j+1]:
                              items[j], items[j+1] = items[j+1], items[j]     # Swap!

b) Insertion

def insertion_sort(items):
      """ Implementation of insertion sort """
      for i in range(1, len(items)):
              j = i
              while j > 0 and items[j] < items[j-1]:
                      items[j], items[j-1] = items[j-1], items[j]
                      j -= 1

c) Merge

def merge_sort(items):
        """ Implementation of mergesort """
        if len(items) > 1:

                mid = len(items) / 2        # Determine the midpoint and split
                left = items[0:mid]
                right = items[mid:]

                merge_sort(left)            # Sort left list in-place
                merge_sort(right)           # Sort right list in-place

                l, r = 0, 0
                for i in range(len(items)):     # Merging the left and right list

                        lval = left[l] if l < len(left) else None
                        rval = right[r] if r < len(right) else None

                        if (lval and rval and lval < rval) or rval is None:
                                items[i] = lval
                                l += 1
                        elif (lval and rval and lval >= rval) or lval is None:
                                items[i] = rval
                                r += 1
                        else:
                                raise Exception('Could not merge, sub arrays sizes do not match the main array')

d) Quick

def quick_sort(items):
      """ Implementation of quick sort """
      if len(items) > 1:
              pivot_index = len(items) / 2
              smaller_items = []
              larger_items = []

              for i, val in enumerate(items):
                      if i != pivot_index:
                              if val < items[pivot_index]:
                                      smaller_items.append(val)
                              else:
                                      larger_items.append(val)

              quick_sort(smaller_items)
              quick_sort(larger_items)
              items[:] = smaller_items + [items[pivot_index]] + larger_items

#10 HAVE PYTHON RELATED QUESTIONS READY TO ASK YOUR INTERVIEWER


Inspired by - sorting was added

More reading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment