c++,time-complexity,virtual-functions,space-complexity

The time complexity to invoke a function is O(1). Each class has a 'vtable', which is essentially a struct of function pointers - each object has a pointer to this vtable, so calling a virtual function is just looking up the function pointer in the vtable and calling it. The...

java,time-complexity,tostring,space-complexity

Time complexity is O(n), where n is the number of nodes, because you iterate over each node once. Space complexity is actually O(n*m), where n is the number of nodes and m is the length of the largest string you will create (which is the last string which contains all...

python,time-complexity,space-complexity

Since you are asking for complexity, let’s take a look at your first solution: The outer loop will run n times, with n being the length of the list. For each iteration, you are then iterating again over the first k elements. So if we just count all the inner...

java,algorithm,recursion,space-complexity

In older versions of Java (mainly in Java 6 and before)*, substring returned a new instance that shared the internal char array of the longer string (that is nicely illustrated here). Then substring had time and a space complexity of O(1). Newer versions use a different representation of String, which...

java,algorithm,complexity-theory,space-complexity

The space complexity accounts for the amount of live memory used by the algorithm, therefore the ArrayList<int[]> factors in to the complexity. However, the algorithm description given at the top of your post says that you only need to print the combinations, not return them; if you immediately print out...

java,algorithm,binary-tree,complexity-theory,space-complexity

You are storing the all the nodes that lie on the path from the root to a particular leaf in the List. If the binary tree is height balanced or it is a full/complete binary tree, the worst case time and space complexity is O(log n), where n is the...

data-structures,asymptotic-complexity,space-complexity,skip-lists

If you don't set an upper bound on the height of the skip list, then there is no upper-bound to the worst-case space usage. For any bound you could place, there's some horribly unlucky and astronomically improbable execution of the skiplist that would result in a number of layers so...

space-complexity,playing-cards

Given no prior information, the theoretical limit on how compactly you can represent an ordered deck of cards is 226 bits. Even the simple naive 6-bits-per card is only 312 bits, so you probably won't gain much by being clever. If you're willing to sacrifice a large part of the...

You cannot get the number of distinct words exactly without paying the space complexity. However, you can get a reasonably good estimate by using the Flajolet-Martin approach, described on slide 20 of this slide deck. Assuming the data stream consists of a universe of elements chosen from a set of...

big-o,complexity-theory,time-complexity,space-complexity

You're totally correct in simplifying O(a+b) = O(a) as per this case. It's so because a>=b (given) O(a+b) <= O(a+a) = O(2a) = O(a) // as clearly mentioned by you. Example :- Let's assume a = n; b = log(n). Then,you can see O(a+b) = O(n+log(n)) = O(n) = O(a)....

java,algorithm,recursion,time-complexity,space-complexity

If anyone else is still confused, be sure to check out this Youtube video that discusses the space complexity of generating the Fibonacci Sequence. Fibonacci Space Complexity The presenter made it really clear why the space complexity was O(n), the height of the recursive tree is n....

python,time-complexity,space-complexity

Without further information on the problem, your actual task and your solving attempts an answer could merely be adequate...but I will try to at least give you some input. a = [random.randint(1,100) for i in xrange(1000000)] A statement like a = ... is normally considered to have O(1) in terms...

algorithm,list,constants,time-complexity,space-complexity

Solving this puzzle requires knowledge of a small trick: XOR-ing a number with itself even number of times results in zero. Moreover, you can XOR other numbers with a temporary result in between; the order does not matter. Therefore, if you XOR all values in the array together, you would...

c++,map,time-complexity,unordered-map,space-complexity

unordered_map worst case usually happens when the hash function is producing collisions for every insertion in the map. I said "usually" because the standard only specifies the worst case complexity, not when or how it will happen, so theoretically the answer to your question is that it is implementation defined....

java,recursion,space-complexity

Neither of them. Objects are not copied when passing their references to parameters of a method invocation, but references itself still occupy stack space, so the space-complexity is O(n) assuming that n refers to the depth of your binary tree. The O(…) notation ignores constant scale factors, therefore O(n) only...

java,algorithm,space-complexity

Well in case m=n, O(m)=O(n), but it is safe to state that the memory complexity is O(m) since it's the only real factor. On the other hand, a HashSet<T> can under extreme circumstances be less memory efficient: after all it uses buckets, the buckets can be filled in a bad...

java,performance,out-of-memory,time-complexity,space-complexity

One solution (for performance) would be to use a SortedSet like so public static List<Integer> intersection2(List<Integer> a, List<Integer> b) { SortedSet<Integer> aSet = new TreeSet<Integer>(a); aSet.retainAll(b); return new ArrayList<Integer>(aSet); } Another solution (for space) would be use the passed in List(s) like so (EDITED with the "new requirement" that the...

Your loop adds at most n-1 key/value pairs to the HashMap. Therefore, the space complexity is O(n), since the HashMap internal storage consists of an array whose size would reach a power of 2 close to n (assuming you didn't give the HashMap an initial capacity that is much larger...

This is essentially a recursive tree traversal and as such it would be O(n). See the following for a good overview: Big-Oh for Recursive Functions

python,python-2.7,time-complexity,space-complexity

map may be microscopically faster in some cases (when you're NOT making a lambda for the purpose, but using the same function in map and a listcomp). List comprehensions may be faster in other cases and most (not all) pythonistas consider them more direct and clearer. more clearly explained here...