Crack Interview Part 1
Crack Interview Part 1
Interviews
Part 1
Author
Arun Arunisto
About the Author
Sample Code
# Create a list of numbers
my_list = [1, 2, 3, 4, 5]
Sample Code :
#creating a new list
new_list = ["arun", "arunisto", "python", "19154Ever"]
#accessing elements
print(new_list[0]) #output : arun
print(new_dict["name"]) #output : arun
#modifying elements
new_list[0] = "arun arunisto"
new_dict["name"] = "arun arunisto"
for loop and pattern
A for loop is used to iterate over a sequence of
elements, such as a list or a tuple, and execute a block
of code for each element in the sequence. The number
of iterations is predetermined by the length of the
sequence.
Sample Code
#pyramid pattern
#odd numbers of "*"
space = 10 - 1
for i in range(1, 10):
for j in range(space):
print(end=" ")
space-=1
if i%2 != 0:
for k in range(i):
print("*", end=" ")
print("\r")
fooBar program using For Loop
#fooBar Program
#divisible by 3 foo
#divisible by 5 bar
#divisible by 3 and 5 fooBar
for i in range(16):
if i%3 == 0 and i%5 == 0:
print("fooBar")
elif i%3 == 0:
print("foo")
elif i%5 == 0:
print("bar")
else:
print(i)
Sample Code
def factorial(n):
if n == 0:
return 1
else:
return n * (factorial(n-1))
print(factorial(5))
Iterator in Python
Iterators allow you to iterate over a collection of
elements one at a time, without having to load the
entire collection into memory at once. This makes them
useful for processing large data sets, where memory is
limited.
In Python, an iterator is an object that implements the
iterator protocol, which consists of two methods:
Sample Code :
my_list = [1, 2, 3, 4, 5, 6]
my_iterator = iter(my_list)
print(next(my_iterator)) #1
print(next(my_iterator)) #2
print(next(my_iterator)) #3
print(next(my_iterator)) #4
print(next(my_iterator)) #5
print(next(my_iterator)) #6
print(next(my_iterator)) #StopIterationError
Generator in Python
In Python, a generator is a type of iterator that
generates a sequence of values using the 'yield'
keyword instead of returning a value with 'return'.
Generators allow you to write iterators more quickly
and easily than defining a class with '__iter__' and
'__next__' methods.
Sample Code :
def square_numbers(n):
for i in range(n):
yield i**2
Sample Code
def main_program(func):
def hello():
func()
print("My age is : ",25)
return hello
@main_program
def hi():
print("My name is arun")
hi()
Multiple Inheritance
Multiple inheritance is a feature in Python that allows a
class to inherit from more than one parent class. This
means that a child class can have multiple base classes,
and it inherits all the attributes and methods of each
parent class.
Multiple inheritance can be a powerful tool for creating
complex object hierarchies and can be used to
compose classes in various ways to create new
functionality. However, it can also lead to some issues,
such as the diamond problem, which occurs when
multiple base classes have a common ancestor class,
leading to ambiguity in method resolution order.
#Sample Code
class Parent1:
def hello():
print("Hello")
class Parent2:
def hi():
print("Hi")
class Child(Parent1, Parent2):
def main():
Child.hi() #function of Parent2
Child.hello() #function of Parent1
obj = Child
obj.main() #calling main function from child
Lambda functions
In Python, lambda is a keyword that is used to define
small, anonymous functions. Anonymous functions are
functions that are defined without a name. The lambda
keyword is followed by the function arguments and a
single expression that is evaluated and returned by the
function.
Sample Code :
#define a lambda function that add two integers
add = lambda x, y: x+y