Sample Interview Questions
PYTHON
Q1: Implement to_bin(n) function -
# Given a number as input write a function that returns a# string with binary representation of a positive integer number
# We would like you to write the algorithm to generate this
# binary representation in string format without library functions
def to_bin(n):
# write your code here
return bin_str
# Sample test cases to verify functionality
assert to_bin(2) == '10'
assert to_bin(7) == '111'
assert to_bin(45) == '101101'
assert to_bin(32) == '100000'
assert to_bin(0) == '0'
Q2: Implement a function to extract out all string literals from a SQL string return them in a sorted list
def extract_string_literals(str):
# write your code here
return str_literal_list
# Sample test cases to verify functionality
assert extract_string_literals("select * from order where orderid = 'OEHKJHFUI' and product = 'Iphone 5s'") = ['Iphone 5s', 'OEHKJHFUI']
assert extract_string_literals("select * from inventory") = []
assert extract_string_literals("select * from order where orderid in ('o1','o2','o1')") = ['o1', 'o1', 'o2']
##################################################################################
# Question 1:
# Complete a function that returns the number of times a given character occurs in the given string
# For example:
# - input string = "mississippi"
# - char = "s"
#
# - output : 4
def return_char_num(input_str, char):
# Code here
return num
##################################################################################
# Question 2:
# Fill in the blanks
#
# Given an array containing None values fill in the None values
# with most recent non None value in the array
#
# For example:
# - input array: [1,None,2,3,None,None,5,None]
#
# - output array: [1,1,2,3,3,3,5,5]
#
def return_filled_array(input_lst):
# Code here
return output_lst
##################################################################################
# Question 3:
# Complete a function that returns a list containing all the mismatched words (case sensitive) between two given input strings
# For example:
# - string 1 : "Firstly this is the first string"
# - string 2 : "Next is the second string"
#
# - output : ['Firstly', 'this', 'first', 'Next', 'second']
def return_mismatched(str1, str2):
# Code here
return mismatched
##################################################################################
# Question:
# Calculate the average word length.
# For the given set of words return the average word length.
def avg_word_length(words):
# Your code here
return result
assert avg_word_length('') == None
assert avg_word_length('ibm') == 3
assert avg_word_length('ibm microsoft') == 6
assert avg_word_length(' Hello World ') == 5
assert avg_word_length('The movie ends with The-end') == 4.6
print('passed')
##################################################################################
# Question:
# Check if a given IP address is valid.
# A valid IP address must be in the form of xxx.xxx.xxx.xxx,
# where xxx is a number from 0-255
# For example:
# - 256.1.2.3 is not a valid IP address because the
# first octet number is greater than 255
# - 245.1.2 is not a valid IP address because
# it has only 3 octets
# Note: We recommend you do NOT use regex to solve this question
# as it can be difficult and time consuming to debug. You may however, do
# so if you wish.
def is_valid_IP(check_ip):
# Your code here
return True
##################################################################################
# implement array_unique(input_list) function -
# Given a list of numbers as input write a function that returns
# a list with duplicates removed.
# Order should be preserved from input based on first occurrence of the element
def array_unique(input_list):
return unique_list
########################################################################
SQL
Given a table: ORDERS with following columns
- CUSTOMER_ID
- DAY_OF_ORDER
- PRODUCT
- PRICE
Q1. Write SQL query to find customers who have bought BOTH products 'iPhone 5s' AND also 'Samsung Note'.
Q2. For all customers who have 2 orders, what is the average number of days elapsed between 1st and 2nd order (do not worry about using exact date functions
/* products sales +------------------+---------+ +------------------+---------+ | product_id | int |------->| product_id | int | | product_class_id | int | +---->| store_id | int | | brand_name | varchar | | +->| customer_id | int | | product_name | varchar | | | | promotion_id | int | | price | int | | | | store_sales | decimal | +------------------+---------+ | | | store_cost | decimal | | | | units_sold | decimal | | | | transaction_date | date | | | +------------------+---------+ | | stores | | customers +-------------------+---------+ | | +---------------------+---------+ | store_id | int |-+ +--| customer_id | int | | type | varchar | | first_name | varchar | | name | varchar | | last_name | varchar | | state | varchar | | state | varchar | | first_opened_date | datetime| | birthdate | date | | last_remodel_date | datetime| | education | varchar | | area_sqft | int | | gender | varchar | +-------------------+---------+ | date_account_opened | date | +---------------------+---------+ */ /* Question: Find the birthdate of the EARLIEST born and LAST born customers, by gender, who have bought at least 1 product */
/* Question: List all the customers that live in Oregon, ordered by the number of unique products they bought. */
/* Question: % of all customers that have purchased at least 1 product? */