Standard Operations & Algorithms - Computer Science

Card 0 of 20

Question

What is the difference between inorder traversal of a binary search tree and a preorder traversal?

Answer

In this case, the names help to identify the different types of traversals. In order traversal processes the binary tree "in order", meaning it will go through the left subtree, process the node, then go on to the right. Why is this in order? Because remember, a binary sort tree lists any value less than the node to the left, and any value greater to the right, so in order traversal will actually go from greatest to smallest.

Preorder traversal is the same except that it processes the root node first, hence the "pre" order.

Compare your answer with the correct one above

Question

public static int foo(int\[\] arr, int x) {

int a = 0;

int b = arr.length - 1;

while(b >= a) {

int c = (a + b) / 2;

int v = arr\[c\];

if(v == x) {

return c;

} else if(v < x) {

a = c + 1;

} else {

b = c - 1;

}

}

return -1;

}

What is the value of y in the code below:

int\[\] vals = {1,3,4,5,6,31,41,51};

int x = 41;

int y = foo(vals,41);

Answer

The first thing to notice in the code for the method foo is that it implements the algorithm for a binary search. The value c functions as the midpoint for the algorithm. This requires that the list be in order. The values a and b are used to control the loop so as to let you search the correct portions of the list. If the value in the middle of the list is not equal to what you are looking for (i.e. x), then it looks either "above" or "below" in the list (since it is presumed to be in order). This is what is happening when either a or b is changed.

So, this searches for the value and returns the index number for that value if it is found. Otherwise, it returns -1. Since the value 41 is in the array, it returns 6.

If you do not recognize this algorithm, you are likely to have some problems—and will have to trace the code!!

Compare your answer with the correct one above

Question

Which of the following is true for a binary search algorithm?

Answer

The binary search presumes that the array being searched is already sorted. This is because of how it probes the values in the array. It begins at the center of the array to see if the values match. If the value is smaller than this center element, it then presumes that the value is in the lower half of the array. Otherwise, it presumes that the value must be larger and hence in the upper half of the array. It then probes the center of whichever half it has chosen, continuing to do this until the value has certainly not been found.

Compare your answer with the correct one above

Question

Which of the following defines a method that successfully deletes an item from an array of integers?

Answer

Of course, this is an inefficient way to do such a delete, but arrays are rather "locked" data structures in that their size cannot change without a reassignment. (You could, of course keep track of the last "used" index. However, that is a different implementation, not reflected here.) The correct answer is the one that carefully goes through the original array, copying those contents into the new array skipping the one value that is not wanted.

Compare your answer with the correct one above

Question

public static boolean remove(int\[\] arr, int val) {

boolean found = false;

int i;

for(i = 0; i < arr.length && !found; i++) {

if(arr\[i\] == val) {

found = true;

}

}

if(found) {

for(int j = i; j < arr.length;j++) {

arr\[j - 1\] = arr\[j\];

}

arr\[arr.length - 1\] = 0;

}

return found;

}

For the code above, what will be the content of the variable arr at the end of execution, if the method is called with the following values for its parameters:

arr = {3,4,4,5,17,4,3,1}

val = 4

Answer

This code simulates the removal of a value from an array by shifting all of the elements after that one so that the array no longer contains the first instance of that value. So, for instance, this code takes the original array {3,4,4,5,17,4,3,1} and notices the first instance of 4: {3,**4**,4,5,17,4,3,1}. Next, it starts shifting things to the left. Thus, some of the steps will look like this:

{3,4,4,5,17,4,3,1}

{3,4,5,5,17,4,3,1}

{3,4,5,17,17,4,3,1}

...

{3,4,5,5,17,4,1,1}

Then, at the very end, it sets the last element to 0:

{3,4,5,17,4,3,1,0}

Compare your answer with the correct one above

Question

int\[\] arr = new int\[20\];

int x = 6,i=2;

for(int j = 0; j < x; j++) {

arr\[j\] = j * 40 + 20;

}

for(int j = x; j > i; j--) {

arr\[j\] = arr\[j - 1\];

}

arr\[i\] = 20;

for(int j = 0; j < x; j++) {

System.out.print(arr\[j\] + " ");

}

What is the output for the code above?

Answer

It is easiest to understand this by a bit of code parsing:

First, there is the loop: for(int j = 0; j < x; j++) { // ...

This will fill the array with 20 + 0, 20 + 40, 20 + 80, ... Thus, it will be:

20, 60, 100, 140, 180, 220

Next, there is the array: for(int j = x; j > i; j--) { // ...

This basically shifts everything after index i backward by 1. Thus you have:

20, 60, 100,100, 140, 180, 220

Next, you set arr\[2\] equal to 20:

20, 60, 20,100, 140, 180, 220

Finally you output each of the first 6 elements. Be careful here. Notice that it is from j = 0 to x - 1!

Thus, the answer is:

20 60 20 100 140 180

This algorithm basically implements a simple kind of array deletion.

Compare your answer with the correct one above

Question

Which of the following statements explains insertion sort?

Answer

Insertion sort removes an element from a list, checks the values adjacent to it to see if it is greater or smaller, until it finds the position where the number to the left is smaller and the number to the right is larger and places it there.

Compare your answer with the correct one above

Question

{1, 9, 5, 4, 2, 0, 4}

What would the set of numbers look like after four iterations of Insertion Sort?

Answer

Insertion Sort is a sorting algorithm that starts at the beginning of an array and with each iteration of the array it sorts the values from smallest to largest.

Therefore, after four iterations of Insertion Sort, the first four numbers will be in order from smallest to largest.

Compare your answer with the correct one above

Question

Which of the following do we consider when choosing a sorting algorithm to use?

I. Space efficiency

II. Run time efficiency

III. Array size

IV. Implementation language

Answer

All of the choices are important when choosing a sorting algorithm. Space and time complexity are the characteristics by which we measure the performance of an algorithm. the array size directly affects the performance of an algorithm. In addition, the language which the algorithm is written in can also affect the performance (for example, insertion sort may run faster in one language versus another, due the way the language was designed).

Compare your answer with the correct one above

Question

public static int[] doWork(int[] arr, int val,int index) {

int[] ret = new int[arr.length + 1];

for(int i = 0; i < index; i++) {

ret[i] = arr[i];

}

ret[index] = val;

for(int i = index + 1; i < ret.length; i++) {

ret[i] = arr[i - 1];

}

return ret;

}

What does the code above perform?

Answer

A quesiton like this is most easily understood by doing a careful reading of the code in question. Let's consider each major section of code:

int[] ret = new int[arr.length + 1];

This line of code creates a new array, one that is 1 longer than the array arr.

for(int i = 0; i < index; i++) . . .

This loop copies into ret the values of arr up to index - 1.

(This is because of the i < index condition)

Then, the code stores the value val in ret[index]

_for(int i = index + 1; i < ret.length; i++) . . ._

It then finishes copying the values into ret.

Thus, what this does is insert a value into the original array arr, returning the new array that is one size larger. (This is necessary because of the static sizing of arrays in Java.)

Compare your answer with the correct one above

Question

public static int[] doWork(int[] arr, int val,int index) {

int[] ret = new int[arr.length + 1];

for(int i = 0; i < index; i++) {

ret[i] = arr[i];

}

ret[index] = val;

for(int i = index + 1; i < ret.length; i++) {

ret[i] = arr[i - 1];

}

return ret;

}

Which of the following is a possible error in the first loop in code above?

I. The array arr might be indexed out of bounds.

II. The array ret might be indexed out of bounds.

III. A null pointer exception might occur.

Answer

The most obvious possible error is that the array arr might be a null value. You need to check for these kinds of values before using the variables. (If you do arr[0] on a null value, an exception will be thrown.) In addition, it is possible that a value for index might be given that is too large. Consider if index = 100 but arr is only 4 elements long. Then, you will have ret be a 5 value array. When the first loop starts to run, you will go all the way to 99 (or at least attempt to do so) for the index value i_; h_owever, once you get to ret[4] = arr[4], there will be an out of bounds error on arr, which only has indices 0, 1, 2, and 3. Of course, there could be other problems later on with ret, but the question only asks about this first loop.

Compare your answer with the correct one above

Question

int\[\] arr = {0,0,0,0,0,0,0,0,0,0};

int arrFill = 0;

int val;

// In here, n items are added. ArrFill is n. Presume that n <= 9

// The variable val now contains a new value to be added

Which of the following blocks of code push a new value into arr as though it were a stack?

Answer

Let us presume that the array arr looks like the following list of integers:

{4,51,41,0,0,0,0,0,0,0}

Presume that our new value is 77.

A stack could either push on to the beginning and move everything back one, giving you something like:

{77,4,51,41,0,0,0,0,0,0}

However, none of the options do this. (No, not even the one that uses the index 0. This one does not add on to the array so much as replace the first element of it.)

So, the other option is to put it on the "end" of the list. The variable arrFill is being used for this purpose. If it is 3, this means that it is the value of the fourth element. Thus, you can set arr\[4\] = 77 (where 4 really is arrFill).

This will give you:

{4,51,41,77,0,0,0,0,0,0}

You also need to add one to the value of arrFill.

The other options do not correctly address the array. They either are too large or too small by one.

Compare your answer with the correct one above

Question

True or False.

The worst case for insertion into an ArrayList is O(N).

Answer

Insertion into an ArrayList is typically O(1). However, since an ArrayList uses an array as its underlying data structure, there can be a need to resize the underlying array. Resizing an array requires creating a new array and copying all the data from the old array to the new array which takes O(N) time.

Compare your answer with the correct one above

Question

Mergesort

The above diagram represents what type of sorting algorithm?

Answer

Mergesort consists of breaking down an unsorted list into subarrays; sorting each sub-array, and recombining the sub-arrays into larger arrays.

Compare your answer with the correct one above

Question

How is merge sort accomplished?

Answer

In merge sort, a list is broken up into sublists containing 1 element. Each element is then compared to another element and sorted. Each 2-element group is then combined with other 2-element groups, comparing the first value of each group and deciding how to four elements. The larger group of four is compared to another group of four, until the process ends and the list is sorted.

Compare your answer with the correct one above

Question

Of the choices below, what is the most efficient sorting algorithm for an unordered list where the size of the list is an odd number and the size of the list is finite?

Answer

Mergesort is the most efficient among the choices. Both selection sort and insertion sort use O(N2) time. Bubble Sort may seem like a good answer but uses O(N2) time most of the time and can be adapted to use O(N) time however only when the list is nearly sorted, so it's a gamble. Mergesort always uses O(NlogN) time and thus is always the most efficient among the four choices.

Compare your answer with the correct one above

Question

What is the worst-case run-time of selection sort (in Big-O notation?)

Answer

Selection sort is comprised of outer and inner for loops that swap elements of the unsorted array into a sorted array. The largest possible number of times each loop can run is the number of elements in the array. Thus, the worst possible run time is .

Compare your answer with the correct one above

Question

True or False.

Selection sort is quicker than MergeSort.

Answer

MergeSort is has a running time of O(N). Selection sort has a running time of O(N2). Selection sort has O(N2) comparisons due to the swap in the algorithm.

Compare your answer with the correct one above

Question

Which of the following implements a method named contains for searching an array sequentially, confirming whether or not the array contains a requested element?

Answer

The basic way to implement a sequential search is to test each element of an array until you match the value you want to find. All of these possible answers are very close to doing this. They all iterate through the given array. They all do check for the value. However, one option (with the if-else logic) could return false even if the element was found, for it continues to run after it is found. If another value does not match later in the array, the variable success will then be set to false. This will be returned, indicating failure to find the value. The integer-returning method seems to be fine, but it would be ambiguous if the value for the search is negative one. In this one case, this return value will not signal necessarily that it has been found—it could be just the "flag" indicating that nothing was found. Thus, the simplest method is the best here: return true as soon as it is found.

Compare your answer with the correct one above

Question

public static int foo(int\[\] arr, int x) {

for(int i = 0; i < arr.length; i++) {

if(arr\[i\] == x) {

return i;

}

}

return -1;

}

Given the method defined above, how many times is the word "Indubitably!" output by the code below?

int\[\] vals = {1,4,51,3,14,91,130,14};

for(int i = 0; i < 20; i++) {

if(foo(vals,i%4) < 0) {

System.out.println("Indubitably!");

}

}

Answer

To make this question much easier, first notice that the foo method implements a sequential search. (This is a search that merely goes through each element of an array and sees if it equals the probe value.) It returns the index if it is found. Otherwise, it returns -1. Now, in the code block in the question itself, the loop iterates for i = 0 to i = 19. It will only execute a search, however, on the values 0...3. This is because of the modulus operator. Thus, it will do (from 0 to 19), 0...3 a total of five times. Thus, it will probe for 1 and 3 five times—these are each in the array. So, the word "Indubitably!" will be output a total of ten times.

Compare your answer with the correct one above

Tap the card to reveal the answer