HackerRank study Flashcards
(153 cards)
Data structures
If we have a tree of n nodes, how many edges will it have?
n-1
Data Structures
Which of the following data structures can handle updates and queries in log(n) time on an array?
Segment Tree
Data structures
Of the following data structures, which has a Last in First Out ordering?(LIFO) In other words, the one that came in last will be the first to be popped.
Stack
A stack works on the principle where the last element added is the first to be removed. It’s like a stack of plates; you remove the top plate first, which was the last one placed
Data structures
Of the following data structures, which has a First In First Out (FIFO) ordering? In other words, the one that came in first will be the first to be removed.
Queue
In a queue, the element that is added first to the collection is the first one to be removed. This ordering is analogous to how people queue up for services, where the first person to line up is the first to receive service and leave.
True or False: “/” is float division.
True
True or False: “//” is integer division.
True
What do you call this expression?
newlist = [expression for item in iterable if condition == True]
list comprehension
Overloading
What is the output of the following Python code snippet?
class Printer:
def print_number(self, a, b=1):
return a + b
p = Printer()
output = p.print_number(5)
6
What does method overloading refer to in Python?
A) Defining multiple methods outside the class with the same name
B) Defining multiple methods within the class with the same name but different parameters
C) Changing the behavior of built-in Python operators
D) Using multiple classes with the same method name
Defining multiple methods within the class with the same name but different parameters
Which of the following is a correct way to simulate method overloading in Python?
A) Using multiple methods with the same name but different numbers of required parameters
B) Using default parameters in methods
C) Defining methods in different modules
D) Python supports method overloading by default
Using default parameters in methods
overloading
Consider the following code:
class Display:
def show(self, message=”Hello”, times=1):
for _ in range(times):
print(message)
What happens when the method show is called as show(“Goodbye”, 3) on an instance of Display?
A) It prints “Hello” 1 time
B) It prints “Goodbye” 3 times
C) It prints “Goodbye” 1 time
D) It results in an error
It prints “Goodbye” 3 times
Which statement about overloading in Python is true?
A) Python allows different methods in the same class to have the same name as long as their parameter types are different.
B) Overloading in Python can only be implemented using external libraries.
C) Python uses the last defined method if multiple methods have the same name and number of parameters.
D) Python natively supports method overloading like Java or C++.
Python uses the last defined method if multiple methods have the same name and number of parameters.
Define O(1)
Constant Time: An algorithm is said to run in constant time if it requires the same amount of time regardless of the input size. For example, accessing any element in an array by index is an O(1) operation.
Define O(n)
Linear Time: An algorithm runs in linear time when the execution time increases linearly with the input size. For example, finding the maximum element in an unsorted array requires looking at each element, resulting in O(n) time complexity.
Define O(log n)
Logarithmic Time: Logarithmic time complexity occurs when the algorithm reduces the input data size by a constant factor (usually half) with each step. Binary search is a classic example, where each step cuts the search space in half, resulting in O(log n) time complexity.
Define O(n^2)
Quadratic Time: This complexity arises when an algorithm involves a double loop over the data. For example, a simple bubble sort on an array has O(n^2) time complexity because it iterates over the array for each element.
Define O(n log n)
This is often seen in more efficient sorting algorithms like quicksort and mergesort, where the algorithm divides the data (log n) and then performs linear work at each division (n).
O(2^n) and O(n!)
These are seen in algorithms that grow extremely fast with input size, often making them impractical for large inputs. Examples include certain recursive algorithms like calculating Fibonacci numbers naively or solving the traveling salesman problem via brute force.
What o notation is this
Accessing an Array Element
def get_first_element(array):
return array[0]
Accessing the first element is an O(1) constant time
What o notation is this
Checking if a Dictionary is Empty
def is_empty(dictionary):
return not dictionary
Checking emptiness is O(1) because it’s a direct property check.
What o notation is this
Incrementing a Number
def increment(number):
return number + 1
Incrementing a number is an O(1) operation.
What o notation is this
Setting a Value in a Dictionary
def set_value(dictionary, key, value):
dictionary[key] = value
Setting an item in a dictionary is O(1) on average.
What o notation is this
Return a Constant
def return_constant():
return 42
Returning a constant is an O(1) operation.
What o notation is this
Checking a Boolean Flag
def check_flag(flag):
if flag:
return “True”
else:
return “False”
Checking a flag is an O(1) operation.