Krossaspurningar fyrir lokapróf í gagnaskipan

¡Supera tus tareas y exámenes ahora con Quizwiz!

What is the time complexity of the following function def func(lis): for i in range(len(lis)): lis[i] += i O(log n) O(n) O(1) O(n^2)

b

What is the time complexity of the following function def func(n): if n == 1: return 0 return 1 + func(n//2) a. O(n) b. O(log n) c. O(1) d. O(n^2)

b

What value will the red node have after we call Add(3) in the heap? a. 4 b. 3 c. 6 c. 7 d. No other option is true

b

Heap-sort is implemented by taking every item in a data structure, adding it to a heapand then removing each item from the heap and adding it back to the original datastructure. The items come off the heap ordered. Knowing the time-complexity of addingto and removing from a heap ( see question 4 ), what is the time complexity of heap-sort? a. O(n2) b. O(n * log n) c. O(n) d. O(log n)

b

If data is entered into a BST in a sequence ordered by key, what will the time complexity of finding a value in the resulting tree be? a. O(log n) b. O(n) c. O(n^2) d. O(1)

b

In a map, implemented with a correct binary search tree, what is the worst-case time complexity of outputting every item in the tree, ordered by the key values (where n is the number of items in the entire tree)? O(n^2) O(n) O(n * log n) O(log n)

b

Look at this recursive program:def foo(a_str, n):if n == 0:return 0n -= 1if a_str[n] == 'a':return 1 + foo(a_str, n)return foo(a_str, n)What does it do? a. Returns the length of the string a_str if 'a' occurs in it, otherwise 0 b. Returns the number of times 'a' occurs in the first n letters of a_str c. Recurs endlessly and never returns d. Returns 1 if 'a' occurs in the first n letters of a_str, otherwise 0

b

Power, part 2/2A programmer is asked to make a function that calculates n to the power of m.This recursive function is what they come up with:def bar(n, m):return n * bar(n, m-1)We test it on the positive integers 2 and 3:print(bar(2, 3))What change is needed for it to work? a. In the recursive call we need to call bar(n, m+1) instead of bar(n, m-1) b. We need to add a condition: if m == 0: return 1 c. No change, it works correctly already d. In the recursive call we need to call bar(n-1, m) instead of bar(n, m-1)

b

Starting with the heap in the image, the next value is removed from the heap. Where does the value 6 end up? a. AA b. BB c. CC d. DD

b

Starting with the heap in the image, the value 3 is added to the heap. Where does the value 3 end up? a. BB b. CC c. DD d. AA

b

Under what circumstances does the remove function for a Arraylist result in the worst case time complexity a. When removing the largest item in the list b. When removing the item at the front end of the list (index: 0) c. When removing the item at the back end of the list (index: len(lis)-1) d. When removing the smallest item in the list

b

What is the maximum number of nodes at a binary tree's n-th level (nodes of depth n)? ___________________________________________________________ a. 2^(n+1) b. 2^(n) c. 3^(n) d. 2^(n-1)

b

What is the time complexity of inserting a value at the back end of a Arraylist (the inserted value will have index:len(lis)) O(n) O(1) O(n^2) O(log n)

b

When a linked list does not have reserved memory for an item that is to be added, the program must: a. stop adding, as you can not add to a linked list after the capacity is used up b. just add another item, as there is no capacity limit on a linked list c. reserve data for several more items in a new memory location d. re-allocate memory for the entire list and copy the data to the new location

b

When designing a recursive function what must be included? a. At least two different code paths with recursion b. A base case condition and a recursive step c. A base case condition, and a return statement d. Only a recursive step

b

Which operations in an efficiently implemented SLL (that has head and tail references) should be used when creating an efficient Queue? a. push_front and pop_back b.push_back and pop_front c. push_back and pop_back d. push_front and pop_front

b

You are given a reference to the head of a SLL as well as a reference to a node somewhere in the list which we'll name n1. What is the time complexity of adding an element behind n1 in the list (closer to tail). O(log n) O(1) O(n) O(n^2)

b

An operation retrieves the value from a single item in an array at a specified location. What is the worst-case time complexity of an efficient implementation? a. O(1) - constant time b. O(log n) - logarithmic time c. O(n) - linear time d. O(n2) - quadratic time

a

Given a correct and efficient implementation of a list class with instance variables referencing both head (or header) and tail (or trailer), removing the first item (furthest away from tail): a. is O(1) in both a singly-linked list and a doubly linked list b. is not possible in a singly-linked list, only in a doubly-linked lis c. is O(n) in both a singly-linked list and a doubly linked list d. is O(n) in a singly-linked list but O(1) in a doubly-linked list

a

Given a correctly implemented hash function used in a hash table implementation -two key instances of equal value will always yield the same hash value -two key instances of different values can not yield the same hash value -all of the other statements are true -two items in the same bucket will always have the same key value

a

Given a map efficiently implemented with hash tables, using a key with a good hash function and in a state where a rebuild() is not needed: -The time complexity of inserting, finding and removing is O(1) -The time complexity of inserting and finding is O(1) while removing is O(n) -The time complexity of inserting is O(1) while finding and removing is O(log n) -The time complexity of inserting is O(1) while finding and removing is O(n)

a

Given an efficient implementation of a DLL with a reference to a node in the list, what is the time complexity of removing the referenced node? a. O(1) b. O(log n) c. O(n) d. O(n^2)

a

In a binary tree of height 4, what is the maximum height of the left subtree of a node? a. 3 b. 2 c. 4 d. 1

a

In an implementation of a dynamic array, once the array is fully used but an additional item must be added, the program must: a. Allocate memory for a bigger array and copy all items between arrays b. Allocate new memory and link that memory with the end of the array c. Add more space at the end of the array, in memory d. Add future items to a different dynamic array

a

In which order are the nodes printed if the tree is traversed postorder? a. ÓFTALPRUH b. HLUTAPRÓF c. HTLFARPÓU d. ÓATLFHUPR

a

Starting with the tree in the image...If the value 21 is added to the BST in the image, where will that value end up? a. A b. B c. C d. D

a

The acronym FIFO (first-in-first-out) is generally used to describe: a. The behavior of a queue b. The behavior of a deque c. The behavior of a stack d. The behavior of a list

a

The hash table in the image is rebuilt to an array of double the size. Which items in the hash table will be added to a bucket with the same index that they had before the array's size is doubled? a. A D and E b. A B and E c. B and C d. only B e. A and E

a

The item F is added to the hash table and gets a hash value of 15. In which bucket does it end up? a. 3 b. 0 c. 1 d. 2

a

The time complexity for fetching a value from a singly-linked list at a specified node (item number n in the list) is: a. O(n) b. O(log n) c. O(1) d. O(n^2)

a

The time complexity for fetching a value from a singly-linked list at a specified node (item number n in the list) is: a. O(n) b. O(log n) c. O(n^2) d. O(1)

a

Under what circumstances does the insert function for a Arraylist result in the best case time complexity a. When inserting an item at the back end of the list (index: len(lis)-1) b. When inserting the largest item in the list c. When inserting the smallest item in the list d. When inserting an item at the front end of the list (index: 0)

a

What is the best way to add a ordered list of numbers to a Binary search tree so that we end up with a well balanced tree? a. Shuffling the list so we add the numbers in a random order b. Starting at both ends of the list and working our way towards its middle from both sides simultaniously c. Adding them in order d. Starting at the middle of the list and working our way towards the both ends of the list simultaniously

a

What is the time complexity of checking if a value exists in an unordered Arraylist O(n) O(1) O(log n) O(n^2)

a

What is the worst case time complexity of removing from a heap? a. O(log n) b. O(n^2) c. O(n) d. O(1)

a

When implementing a map using a regular list or array -All of the other statements are true -By implementing the insertion in O(n) the time complexity of retrieving data can be implemented O(log n) -If the time complexity of inserting data is O(1) the time complexity of retrieving must be at least O(n) -The time complexity of retrieving data by a key can never be O(1)

a

Which of the following statements about recursive functions is true? a. None of the other options are true b. Recursion always yields the computationally fastest solution c. Some problems can only be solved using recursion d. Recursion always yields neater solutions than other methods

a

Which operations in an efficiently implemented deque created using a DLL should be used when creating an efficient Stack? a. Either push_front/back and the pop function on the same side of the deque (both work) b. push_front and pop_front c. A queue can't be implemented in a deque d. push_back and pop_back

a

A function is recursive and runs and returns correctly. Which of the following statements made about any such function is not true? a. Every time it calls itself it does so with different parameter values than the current instance b. When the parameter values in the current instance of the function meet certain conditions the recursion will stop c. None of the other options are true d. Called with certain values, it might actually not recur (call itself again)

c

Adding items to a heap and removing them are operations that both have timecomplexity O(log n). Why? sequence. a. The number of comparisons is at most the number of nodes in the lowest level of the tree, which is the base b. Adding new nodes to a heap follows a logarithmic 2 logarithm of n, where n is the number of nodes in a full tree of that height. c. The number of swaps is at most the height (# of levels) of the tree and the height is always close to the base 2 logarithm of n, where n is the number of nodes in the tree. d. The value of the root in a heap will always be at most the base 2 logarithm of the value of any other node.

c

An operation inserts a single item into the beginning (index:0) of an array. What is the worst-case time complexity of an efficient implementation? a. O(1) - constant time b. O(log n) - logarithmic time c. O(n) - linear time d. O(n2) - quadratic time

c

An operation removes a single item from an array at a specified location. What is the worst-case time complexity of an efficient implementation? a. O(1) - constant time b. O(log n) - logarithmic time c. O(n) - linear time d. O(n2) - quadratic time

c

Consider the following hash function for strings: def hash(s): hsh = 0 for i in range(len(s)): hsh += ord(s[i]) # changes character ascii value to int return hsh a. The hash function can return the same hash value for different strings and is therefore useless in hash tables b. The hash function will always return different hash values for different strings and is therefore excellent for use in hash tables c. The hash function can return the same hash value for different strings but hash tables using it will still be correct d. The hash function will always return the same hash value for every string and is therefore useless in hash tables

c

Given a correct and efficient implementation of a list class with instance variables referencing both head (or header) and tail (or trailer), removing an item at a specified integer location in the list: a. is not possible in a singly-linked list, only in a doubly-linked list b. is O(1) in both a singly-linked list and a doubly linked list c. is O(n) in both a singly-linked list and a doubly linked list d. is O(n) in a singly-linked list but O(1) in a doubly-linked list

c

Given a hash function with perfect distribution, what is the expected number of items in each bucket in an array of 16 buckets, if 32 random keys are inserted? 64 1 2 32

c

Given the tree in the above image, which traversal will give the order C, D, B, A, F, G, E? a. Inorder b. Postorder c. None of the other options will give this order d. Preorder

c

If data is entered into a BST in a sequence ordered by key values, what will the worst-case time complexity of finding an item in the resulting tree be (where n is the number of items in the entire tree)? a. O(log n) b. O(1) c. O(n) d. O(n^2)

c

If data is entered into a hashmap using a hash function that returns the same hash for all key values, what will the time complexity of finding a value in the resulting hashmap be? a. O(1) b. O(log n) c. O(n) d. O(n^2)

c

In a correct and full binary search tree what is the worst-case time complexity of finding a particular item (where n is the number of items in the entire tree)? a. O(n) b. O(1) c. O(log n) d. O(n^2)

c

In an Arraylist, what is the time complexity of the pop functions The pop function removes one element from the back end of a list. O(n) O(n^2) O(1) O(log n)

c

In order to access the next-to-last node in a singly-linked list the most efficient possible way for a program is to: a. number of nodes between the current location and the tail (last node) (O(log n)) b. go from the tail (last node) to its previous node in one step (O(1)) c. walk the entire list from the head (first node), one step at a time (O(n)) d. index directly to it using it's index in the list (O(1))

c

Look at the image of a binary search tree. What is the output of printing its contents inorder? a. 7 3 2 5 9 8 b. 3 2 5 7 9 8 c. 2 3 5 7 8 9 d. 7 3 9 2 5 8

c

Power, part 1/2A programmer is asked to make a function that calculates n to the power of m.This recursive function is what they come up with:def bar(n, m):return n * bar(n, m-1)We test it on the positive integers 2 and 3:print(bar(2, 3))How will it behave? a. It will return 2 b. It will return 6 c. It will recur endlessly and never return d. It will return 8

c

Starting with the tree in the image...After removing the value 12 from the tree, which value could be in the root of the tree? a. 15 b. None of the other options could be the value in the root of the tree. c. 22 d. 10

c

Starting with the tree in the image...After removing the value 22 from the tree, which value could be in the root of the tree? a. 10 b. None of the other options could be the value in the root of the tree. c. 27 d. 12

c

Starting with the tree in the image...If the value 36 is added to the BST in the image, where will that value end up? a. A b. B c. C d. D

c

Sum of natural numbers, part 2/2A programmer is asked to make a function that returns the sum of the first n natural numbers.This recursive function is what they come up with:def bar(n):if n == 1:return nreturn n + bar(n)We test it on the natural number 6:print(bar(6))What change is needed for it to work? a. If the condition, n == 1, is true it should return 0 instead of n b. In the if condition, we should check if n == 0 instead of n == 1 c. In the recursive call we need to call bar(n-1) instead of bar(n) d. No change, it works correctly already

c

The following can be limiting factors on how many iterations a recursive function can do: a. The maximum value of a memory location b. The maximum size of the return value c. The maximum space allowed for storing descriptions of currently active function calls d. The maximum amount of parameters that can be sent to a function

c

What is the maximum number of nodes in a binary tree (the whole tree) of height 3? a. 16 b. 15 c. 7 d. 8

c

What is the output of the following program? tail = Node("D", None) head = Node("A", Node("B", Node("C", tail))) node = head head = tail tail = node head.next = tail.next tail.next = None print_list(head) a. D b. D B C A c. the program crashes because of incorrect or looping next pointers d. D C B A

c

What is the output of the following program? tail = Node("D", None) head = Node("A", Node("B", Node("C", tail))) node = head head = tail tail = node print_list(head) a. D C B A b. D B C A c. D d. the program crashes because of incorrect or looping next pointers

c

What is the time complexity of the following function def reverse_list(lis): reversed_lis = [ ] for i in range(len(lis)): reversed_lis.insert(0, lis[i]) return reversed_lis O(n * log n) O(n^3) O(n^2) O(n)

c

Which of the following statements are true about a correctly implemented regular map? a. The data returned for a specific key will have the exact same values as what was last entered or updated using that particular key value. b. After having removed an item using a particular key, that key can still be used again to add another item to the map. c. All of the other statements are true. d. When a key already exists either the data associated with it must be overwritten, or the data being sent in ignored. There will not be a place for both.

c

Which operations in an efficiently implemented SLL (that has head and tail references) should be used when creating an efficient Stack? a. push_back and pop_back b. push_back and pop_front c. push_front and pop_front d. push_front and pop_back

c

A hash function for a string that takes the exclusive or (XOR) of all the character values -has the disadvantage that different permutations of same letters (i.e. "tops", "pots" & "stop") will simply give the same hash value, causing collisions. -has the benefit of being computationally quick while taking all information in the string into consideration. -All of the other statements are true. -is O(n) in the length of the string.

c.

A well implemented deque (double-ended queue) can substitute the behavior of: a) neither a stack nor a queue b) a queue but not a stack c) a stack but not a queue d) both a stack and a queue

d

A well implemented deque created using a DLL can substitute the behavior of: a. a queue b. Neither a stack nor a queue c. a stack d. both a stack and a queue

d

Given a correct and efficient implementation of a list class with instance variables referencing both head (or header) and tail (or trailer), removing the last item (furthest away from head) a. is O(1) in both a singly-linked list and a doubly linked list b. is O(n) in both a singly-linked list and a doubly linked list c. is not possible in a singly-linked list, only in a doubly-linked list d. is O(n) in a singly-linked list but O(1) in a doubly-linked list

d

Given a single variable referencing a particular node in a list, adding a new node into the list directly in front of the referenced node (closer to head (or header)): a. is O(n) in a singly-linked list but O(1) in a doubly-linked list b. is O(1) in both a singly-linked list and a doubly linked list c. is O(n) in both a singly-linked list and a doubly linked list d. is not possible in a singly-linked list, only in a doubly-linked list

d

Given an efficient implementation of a SLL with both head and tail variables, what is the time complexity of removing the tail node of the list? a. O(log n) b. O(n^2) c. O(1) d. O(n)

d

Given the tree in the above image, which traversal will give the order A, B, C, D, E, F, G? ___________________________________________________________ a. None of the other options will give this order b. Postorder c. Inorder d. Preorder

d

If the item '4' were added to the BST in the image, where would it end up in the tree? a. Right child on '2' b. Right child on '9' c. Left child on '8' d. Left child on '5'

d

In order to access the next-to-last node in a singly-linked list the most efficient possible way for a program is to: a. index directly to it using it's index in the list (O(1)) b. jump from the head (first node), in steps of n/2 nodes at a time, where n is the number of nodes between the current location and the tail (last node) (O(log n)) c. go from the tail (last node) to its previous node in one step (O(1)) d. walk the entire list from the head (first node), one step at a time (O(n))

d

Order the following time complexities from fastest(left) to slowest(right) a. O(1) O(n*log n) O(n^2) O(log n) O(n) b. O(1) O(n) O(log n) O(n^2) O(n*log n) c. O(1) O(log n) O(n) O(n^2) O(n*log n) d. O(1) O(log n) O(n) O(n*log n) O(n^2) e. O(1) O(log n) O(n*log n) O(n) O(n^2)

d

Starting with the heap in the image, first the next value is removed, then the value 3 is added to the heap. Where does the value 3 end up? a. AA b. BB c. CC d. DD

d

Starting with the tree in the image...If the value 108 is added to the BST in the image, where will that value end up? a. A b. B c. C d. D

d

Sum of natural numbers, part 1/2A programmer is asked to make a function that returns the sum of the first n natural numbers.This recursive function is what they come up with:def bar(n):if n == 1:return nreturn n + bar(n)We test it on the natural number 6:print(bar(6))How will it behave? a. It will return 1 b. It will return the sum of the first n-1 natural numbers c. It will return the sum of the first n natural numbers d. It will recur endlessly and never return

d

The acronym LIFO (last-in-first-out) is generally used to describe: a. The behavior of a list b. The behavior of a queue c. The behavior of a deque d. The behavior of a stack°

d

The answer to this question is on the Canvas page "Kafli 10: Frekara lesefni og myndbönd". This is important to understand because getting it wrong will not affect the correctness of a hash tables implementation, but it will completely ruin the efficiency (time complexity O(n) vs. O(1)). Which of the following statements are true? -self.bucket_list = [Bucket()] * self.bucket_count will create several instances of Bucket (self.bucket_count many) -I messed up when creating the quiz and had two of the same choice. Ignore this field. -self.bucket_list = [Bucket for _ in range(self.bucket_count)] will only ever create one instance of Bucket -self.bucket_list = [Bucket()] * self.bucket_count will only ever create one instance of Bucket

d

The time complexity for adding a new node at the front of a linked list is: a. O(n) b. O(n^2) c. O(log n) d. O(1)

d

What does the capacity variable in a dynamic array implementation represent? a. How many items have been added into the array b. How many more items can be added into the array c. How many more items can be added into the array before its next resize is necessary d. How many items, in total, the array can hold, before its next resize is necessary

d

What is the output of the following program? head = Node("A", Node("B", Node("C", Node("D", None)))) node = head if node != None: while node.next != None: node = node.next node.next = head head = head.next node.next.next = None print_list(head) a. the program crashes because of incorrect or looping next pointers b. D A B C c. D C B A d. B C D A

d

Which of the following statements is true about a map (dictionary)? a. If a requested key does not exist it will be generated and a default value returned. b. A map will always have space reserved for each key in the set of possible keys. c. If two keys have the same value their contents will be combined. d. None of the other statements are true.

d

Which of the following statements is true? a. Random is a good asset in hash functions because the distribution of true random is very good. b. -A hash function can not use random as part of its calculation because the distribution of true random is not good enough. c.None of the other statements are true. d. A hash function can not use random as part of its calculation because it must return the exact same value for equal keys.

d

Which operations in an efficiently implemented deque created using a DLL should be used when creating an efficient Queue? a. push_front and pop_back b. push_back and pop_front c. A queue can't be implemented in a deque d. Either push_front/back and the pop function on the opposite side of the deque (both work)

d

You are given a reference to the head of a SLL as well as a reference to a node somewhere in the list which we'll name n1. What is the time complexity of adding an element before n1 in the list (closer to head). O(n^2) O(1) O(log n) O(n)

d

A data structure has the following operations that add and remove to and from the front and back of the structure, respectively: 1) push_front(value): #time complexity: O(1) 2) push_back(value): #time complexity: O(1) 3) pop_front(): #time complexity: O(1) 4) pop_back(): #time complexity: O(n) Which operations should be used for the most efficient implementation of a stack? ans: number, number

1, 3

A data structure has the following operations that add and remove to and from the front and back of the structure, respectively: 1) push_front(value): #time complexity: O(1) 2) push_back(value): #time complexity: O(1) 3) pop_front(): #time complexity: O(1) 4) pop_back(): #time complexity: O(n) Which operations should be used for the most efficient implementation of a queue? ans: number, number

2, 3

Comparing arrays and linked lists, which statement is true? a. Data in linked lists is stored in contiguous (samliggjandi) memory locations but not arrays b. Data in arrays is stored in contiguous (samliggjandi) memory locations but not linked lists c. Data in both linked lists and arrays is stored in contiguous (samliggjandi) memory locations d. Data in neither linked lists nor arrays is stored in contiguous (samliggjandi) memory locations

b

Given a single variable referencing a particular node in a list, adding a new node into the list directly behind the referenced node (closer to tail (or trailer)): a. is O(n) in both a singly-linked list and a doubly linked list b. is O(1) in both a singly-linked list and a doubly linked list c. is O(n) in a singly-linked list but O(1) in a doubly-linked list d. is not possible in a singly-linked list, only in a doubly-linked list

b

A function is recursive and runs and returns correctly. Which statement is true? a. It must call a different function in at least one code path b. It has a call to itself in every code path c. It calls itself with unchanged parameter values in at least one code path d. It has at least one code path without a call to itself

d

What value will the blue node have after we call Remove() two times in a row in the heap? a. 21 b. No other option is true c. 8 d. 6 e. 9

e


Conjuntos de estudio relacionados

TExES Science 7-12 Review Edited, TeXes Science 7-12 Practice Test, Texes Composite Science 7-12 (236)

View Set

(165) Machine Learning - Polynomial Regression

View Set

Lesson 7: Creation and Termination of Agency

View Set

Data Collection, Behavior, and Decisions

View Set

Developed vs. Developing Countries

View Set

French: How to ask how someone is doing.

View Set