forCodingInterviews Flashcards

1
Q

if there are duplicate elements x in a list, then what does a.remove(‘x’) remove?

A

the first occurrence of x

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

finding the intersection of two sets

A

result = s1.intersection(s2) s1 & s2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

finding the union of two sets

A

resul = s1.union(s2) or s1 | s2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

how to iterate over a set?

A

for val in set: print(val)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

convert a set into a list

A

list(s)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

set membership

A

if element in set

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

dict membership

A

id key in d

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

list membership

A

if element in list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what does a filter function return and what’s one way to make it usable

A

it returns a filter object and can convert into list using list(filter(function, iterable))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

can you use a filter object with a set?

A

yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

how to create a list of tuples from a dictionary such that a tuple is (key, value)?

A

list(dict.items());

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

adding an element to a tuple

A

remmeber tuples are immutable so t2 = t1 + (newElement,) REMEMBER THE COMMA AT THE END. This is creating a new tuple since tuples are immutable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

inserting element at a particular location n in a tuple

A

t3 = t2[:n] + (‘a’,) + t2[n:]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

removing an element froma particular location n in a tupel

A

t3 = t1[:n] + t1[n+1:]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what objects pass by reference?

A

all mutable objects, i.e. list, set and dict. Non mutable objects are passed by value so any changes to those args inside the function will not be reflected outside.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

adding elements to a set

A

a.add(2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

whats the difference between b=a and b = a[:] where a is a list

A

b=a –> any changes to a will be reflected in b
b = a[:] –> creates a copy of a so changes in a will not be reflected
for nested lists use deepcop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

what are the largest and smallest numbers in python?

A

float(‘inf’) and float(‘-inf’)

19
Q

how do you replace a char c in a string s with char d.

A

s.replace(c,d)

20
Q

what is time complexity of python sort function

A

nlogn - uses Timsort

21
Q

creating permutations (combinations) in python

A

from itertools import permutations
p = permutations(list,r)
for combination use combinations
returns a list of tuples, each containing a permutation (or combination)

22
Q

check if n is power of 2

A

n & (n-1) == 0 and n not 0

23
Q

check if power of 4

A

check for power of 2 and (n-1) % 3 == 0

24
Q

if s is a string what does sorted(s) do?

A

splits characters of s into a list and sorts it. If there are numbers they come first

25
Q

how to convert a string of 0s and 1s into a base 10 integer?

A

i = int(s,2) assuming it’s currently binary

26
Q

how to convert all chars of a string to upper case?

A

s.upper()

27
Q

how to convert all chars of a string to lower case?

A

s.lower()

28
Q

how to ascertain if all chars of a string are digits?

A

s.isdigit()

29
Q

how to convert all chars of a string are alphabets?

A

s.isalpha() will return false even if there is one space

30
Q

convert a character is ascii representation?

A

ord(char)

31
Q

convert an integer to a char using ascii representation

A

chr(int)

32
Q

convert an integer into a string representation

A

str(int)

33
Q

how to create a named tuple

A

from collections import namedtuple
Node = namedtuple(“Node”, [“key”, “value”])
new_node = Node(‘k’, 1)

34
Q

when to use == and when to use “is”

A

except when checking if a variable is None, use ==

35
Q

what is the default data types of python division operation?

A

float

36
Q

what if you want python to return result of division in integer type and not float?

A

use //

37
Q

if you want python to evaluate default arguments fresh everytime it is called, what coding pattern to use?

A
func (default  =None):
   if not default: default = desired_value
38
Q

what objects are hashable in python

A

all immutale objects specifically strings, tuples, integers. All mutable objects such as list, set and dict are not hashable.

39
Q

how to sort a list based on a function f of it’s values (rather than the value itself)

A

a.sorted(key = lambda x: f(x))

40
Q

s1 nd s2 are two sets; get their symmetric difference. elements in set s1 only or s2 only.

A

A ^ B

41
Q

python random number generator

A

randrange(a,b,step); random number will be equal step a < b. However to get random element from list use choice(list)

42
Q

create a dictionary with default value empty list

A

d = defaultdict(list)

43
Q

creating a nested default dictionary

A

defaultdict(lambda: defaultdict(list))

44
Q

create a list with cumulative sums

A

from itertools import accumulate