Data Structures and Algorithms
Tutorial Problems

Short Questions

You should be able to answer most of these questions in a single sentence. Sometimes a single word or expression will suffice.
1 2n2+n is O(n3). True or false?  
2 What does the statement: "f(n) is O(g(n))" mean?  
3 Arrange these functions in order of their growth rates (slowest growing functions first):
  1. log n
  2. 1.0005n
  3. n2
  4. n!
  5. sqrt(n)
  6. nlog n
 
4 For what values of n is
4 x 106 n2 > 10 x 2n?
 
5 What is meant by:
s is an O(1) sequence of statements.
 
6 What is the time complexity of this fragment of code:
for( j=0;j<n;j++ )
    for(k=0;k<n;k+=10) s;
s is an O(1) sequence of statements.
 
7 What is the time complexity of this fragment of code:
for( j=0;j<n;j=j*1.1 )
    for(k=0;k<n;k++) s;
s is an O(1) sequence of statements.
 
8 What attributes are needed for the class(es) needed to model the simplest possible list?  
9 For the simplest possible list, what is the time complexity for:
  1. adding to the head,
  2. adding to the tail,
  3. deleting a specific item,
  4. deleting an arbitrary item,
  5. searching for an item?
 
10 What modifications (if any) can you make to the simplest possible list to change any of the time complexities in the previous question?  
11
  1. What structure do you use to store data so that you can use the binary search algorithm?
  2. How must the data be ordered in this structure?
  3. What is the time complexity for binary search?
  4. Is this time complexity guaranteed?
  5. What is the critical property of the data structure used which allows the binary search algorithm to work?
  6. Can you use the binary search algorithm on a list? Explain your answer in one sentence.
 
12 When using an array as a data structure:
  1. What are the advantages?
  2. What are the disadvantages?
 
13 Stacks
  1. List two ways that you could implement a stack.
  2. Which is the most common?
  3. Why is this method used, despite its limitations?
  4. A stack provides FIFO semantics. True or false?
 
14 For simple binary search tree, what is time complexity for:
Operation Usual O() Guaranteed?
Yes/No
Worst case O()
  • adding an item
  •    
  • deleting an item
  •    
  • searching for an item
  •    
    What does the usual behaviour depend upon?
     
    15 A binary tree has n nodes. What are the bounds on the height of the tree?  
    16
    1. Why is a complete tree important?
    2. What is its critical property?
     
    17 For a heap, what is the time complexity for:
    Operation Usual O() Guaranteed?
    Yes/No
    Worst case O()
    Adding an item   
    Deleting an item   
    Searching for an item   
     
    18 Heaps
    1. What would be the
      1. minimum,
      2. maximum
      number of elements in a heap of height h?
    2. Where in a heap would I find the smallest element?
    3. Is an array that is sorted in reverse order a heap?
    4. Is { 23, 17, 14, 6, 13, 10, 1, 5, 7, 12 } a heap?
     
    19 Sorting time complexity: fill in the table.
    Sort Algorithm Best O() Guaranteed?
    Yes/No
    Usual O() Worst case O() Conditions for best or worst case
  • Bubble
  •      
  • Insertion
  •      
  • Heap
  •      
  • Quick
  •      
     
    20 Sorting
    1. What is the space complexity of the "standard" recursive quicksort?
      Hint: Consider the stack frames used.
    2. When would you prefer heap sort to quick sort?
    3. When would you prefer quick sort to heap sort?
    4. Suggest three situations in which bubble or insertion sort might be used effectively.
      Give a one sentence explanation in each case.
     

    Longer Questions

    1. Algorithm A requires 200 machine cycles for each iteration and requires nlogn iterations to solve a problem of size n.

      A simpler algorithm, B, requires 25 machine cycles for each iteration and requires n2 iterations to solve a problem of size n.

      Under what conditions will you prefer algorithm A over algorithm B?

    2. Simple ADT Design
      A double-ended queue or deque is one that has both LIFO and FIFO behaviour, ie you can add an item to the head or the tail of a list and extract an item from the head or the tail.

      Taking the following specification for the Collection class, modify it to handle a deque. Note:

      Similarly, modify the implementation to handle a deque.

      /* Specification for Collection */
      
      typedef struct t_Collection *Collection;
      
      Collection ConsCollection( int max_items, int item_size );
      /* Construct a new Collection
         Pre-condition: max_items > 0
         Post-condition: returns a pointer to an empty Collection
      */
      
      void AddToCollection( Collection c, void *item );
      /* Add an item to a Collection
         Pre-condition: (c is a Collection created by a call to
                             ConsCollection) &&
                        (existing item count < max_items) &&
                        (item != NULL)
         Post-condition: item has been added to c
      */
      
      void DeleteFromCollection( Collection c, void *item );
      /* Delete an item from a Collection
         Pre-condition: (c is a Collection created by a call to
                           ConsCollection) &&
                        (existing item count >= 1) &&
               	  (item != NULL)
         Post-condition: item has been deleted from c
      */
      
      void *FindInCollection( Collection c, void *key );
      /* Find an item in a Collection
         Pre-condition: c is a Collection created by a call to
                           ConsCollection
                        key != NULL
         Post-condition: returns an item identified by key if one
                         exists, otherwise returns NULL
      */
      
      /* Linked list implementation of a collection */ #include /* calloc */ #include /* NULL */ #include /* Needed for assertions */ #include "collection.h" /* import the specification */ extern void *ItemKey( void * ); struct t_node { void *item; struct t_node *next; } node; struct t_collection { int size; /* Needed by FindInCollection */ struct t_node *node; }; collection ConsCollection(int max_items, int item_size ) /* Construct a new collection Pre-condition: (max_items > 0) && (item_size > 0) Post-condition: returns a pointer to an empty collection */ { collection c; /* Although redundant, this assertion should be retained as it tests compliance with the formal specification */ assert( max_items > 0 ); assert( item_size > 0 ); c = (collection)calloc( 1, sizeof(struct t_collection) ); c->node = (struct t_node *)0; c->size = item_size; return c; } void AddToCollection( collection c, void *item ) /* Add an item to a collection Pre-condition: (c is a collection created by a call to ConsCollection) && (existing item count < max_items) && (item != NULL) Post-condition: item has been added to c */ { struct t_node *new; assert( c != NULL ); assert( item != NULL ); /* Allocate space for a node for the new item */ new = (struct t_node *)malloc(sizeof(struct t_node)); /* Attach the item to the node */ new->item = item; /* Make the existing list `hang' from this one */ new->next = c->node; /* The new item is the new head of the list */ c->node = new; assert( FindInCollection( c, ItemKey( item ) ) != NULL ); } void DeleteFromCollection( collection c, void *item ) /* Delete an item from a collection Pre-condition: (c is a collection created by a call to ConsCollection) && (existing item count >= 1) && (item != NULL) Post-condition: item has been deleted from c */ { struct t_node *node, *prev; assert( c != NULL ); /* The requirement that the collection has at least one item is expressed a little differently */ assert( c->node != NULL ); assert( item != NULL); /* Select node at head of list */ prev = node = c->node; /* Loop until we've reached the end of the list */ while( node != NULL ) { if ( item == node->item ) { /* Found the item to be deleted, re-link the list around it */ if( node == c->node ) /* We're deleting the head */ c->node = node->next; else prev->next = node->next; /* Free the node */ free( node ); break; } prev = node; node = node->next; } }
    Continue on to Tutorials: Part 3 Back to the Table of Contents
    © John Morris, 2004