time-complexity

Replacing loop variables requires time (when instantiating a wrapper)

Replacing loop variables requires time (when instantiating a wrapper) Question: Why can’t I instantiate this List wrapper in constant time? The list is already created (and not a generator), and I’m just saving it in my class. Lists are passed by reference, so please explain. I’m really confused, and can’t seem to find answers. UPDATE: …

Total answers: 1

Next Bigger Number when given an int

Next Bigger Number when given an int Question: I’m having trouble optimizing my algorithm for finding the next biggest number with the same digits when given an integer, returning -1 if there is no bigger number. Examples of what the function should do: next_bigger(13): —> 31 next_bigger(201): —> 210 next_bigger(2017): —-> 2071 next_bigger(10) —–> -1 …

Total answers: 3

Python set iteration time complexity

Python set iteration time complexity Question: What is the time complexity of the following python code? create set x add n items in x remove n items in x add 1 item in x iterate x m times import time def test(n, m): answer = 0 start = time.time() x = set([i for i in …

Total answers: 1

Time Complexity of this program solving the coin change problem

Time Complexity of this program solving the coin change problem Question: I have created a program shown below, and I am confused as to how to figure out its time complexity. Is it O(ntarget/min(coins)) because a for loop is created each time the function is called, and the function is called target/min(coins) times? The program …

Total answers: 1

Time complexity of recursion of multiplication

Time complexity of recursion of multiplication Question: What is the worst case time complexity (Big O notation) of the following function for positive integers? def rec_mul(a:int, b:int) -> int: if b == 1: return a if a == 1: return b else: return a + rec_mul(a, b-1) I think it’s O(n) but my friend claims …

Total answers: 4

Space Complexity with no variables defined

Space Complexity with no variables defined Question: Suppose I had some code: def foo(forest: list[list[int]]) -> int: return sum([1 for tree in forest for leaf in tree]) In this code, no variables are defined. Everything is evaluated in one line, which means to my knowledge the data isn’t being stored anywhere. Does that mean this …

Total answers: 1

A more efficient solution for balanced split of an array with additional conditions

A more efficient solution for balanced split of an array with additional conditions Question: I appreciate your help in advance. This is a practice question from Meta’s interview preparation website. I have solved it, but I wonder if any optimization can be done. Question: Is there a way to solve the following problem with a …

Total answers: 2

The Minion Game

The Minion Game Question: I need some help with the code for the below Minion Game: Kevin and Stuart want to play the ‘The Minion Game’. Game Rules Both players are given the same string, Both players have to make substrings using the letters of the string. Stuart has to make words starting with consonants. …

Total answers: 1