- Q1:Given two arrays, write a python function to return the intersection of the two? For example, X = [1,5,9,0] and Y = [3,0,2,9] it should return [9,0]
- Q2 :Given an array, find all the duplicates in this array? For example: input: [1,2,3,1,3,6,5] output: [1,3]
- Q3: Given an integer array, return the maximum product of any three numbers in the array?
- Q4: Given an integer array, find the sum of the largest contiguous subarray within the array. For example, given the array A = [0,-1,-5,-2,3,14] it should return 17 because of [3,14]. Note that if all the elements are negative it should return zero.
- Q5: Define tuples and lists in Python What are the major differences between them?
- Q6: Compute the Euclidean Distance Between Two Series?
- Q7: Given an integer n and an integer K, output a list of all of the combination of k numbers chosen from 1 to n. For example, if n=3 and k=2, return [1,2][1,3],[2,3]
- Q8: Write a function to generate N samples from a normal distribution and plot them on the histogram
Q1: Given two arrays, write a python function to return the intersection of the two? For example, X = [1,5,9,0] and Y = [3,0,2,9] it should return [9,0]
Answer:
set(X).intersect (set(Y))
Q2: Given an array, find all the duplicates in this array? For example: input: [1,2,3,1,3,6,5] output: [1,3]
Answer:
set1=set()
res=[]
for i in list:
if i in set1:
res.append(i)
else:
set1.add(i)
print(res)
Answer:
import heapq
def max_three(arr):
a = heapq.nlargest(3, arr) # largerst 3 numbers for postive case
b = heapq.nsmallest(2, arr) # for negative case
return max(a[2]*a[1]*a[0], b[1]*b[0]*a[0])
Q4: Given an integer array, find the sum of the largest contiguous subarray within the array. For example, given the array A = [0,-1,-5,-2,3,14] it should return 17 because of [3,14]. Note that if all the elements are negative it should return zero.
def max_subarray(arr):
n = len(arr)
max_sum = arr[0] #max
curr_sum = 0
for i in range(n):
curr_sum += arr[i]
max_sum = max(max_sum, curr_sum)
if curr_sum <0:
curr_sum = 0
return max_sum
Answer:
Lists: In Python, a list is created by placing elements inside square brackets [], separated by commas. A list can have any number of items and they may be of different types (integer, float, string, etc.). A list can also have another list as an item. This is called a nested list.
- Lists are mutable
- Lists are better for performing operations, such as insertion and deletion.
- Lists consume more memory
- Lists have several built-in methods
Tuples: A tuple is a collection of objects which ordered and immutable. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.
- Tuples are immutable
- Tuple data type is appropriate for accessing the elements
- Tuples consume less memory as compared to the list
- Tuple does not have many built-in methods.
- Mutable = we can change, add, delete and modify stuff
- Immutable = we cannot change, add, delete and modify stuff
Q7: Given an integer n and an integer K, output a list of all of the combination of k numbers chosen from 1 to n. For example, if n=3 and k=2, return [1,2],[1,3],[2,3]
Answer
from itertools import combinations
def find_combintaion(k,n):
list_num = []
comb = combinations([x for x in range(1, n+1)],k)
for i in comb:
list_num.append(i)
print("(K:{},n:{}):".format(k,n))
print(list_num,"\n")
Q8: Write a function to generate N samples from a normal distribution and plot them on the histogram
Answer: Using bultin Libraries:
import numpy as np
import matplotlib.pyplot as plt
x = np.random.randn((N,))
plt.hist(x)
