1. What do you understand by a declaration, a definition, and an instantiation?

2. What are the four basic member functions of a C++ class that are defined by the compiler by default?

3. What does the default constructor do (when you don’t write one)? When would you need to redefine a constructor for a class?

4. What are the scenarios when a copy constructor is invoked?

5. How do you decide whether to pass an argument by a pointer or a reference?

6. When you are implementing an operator overloading, how do you decide whether you want it to be a member function or a non-member function?

7. Could you point out a flaw in the following class declaration?

class A { public: void erase() { delete this; } private: int data; };

8. Discuss the different usages of const keyword in C++.

9. Why do you need the following wrapper around a class declaration in the header file?

#ifndef BLAHBLAH_H #define BLAHBLAH_H class ... #endif

10. When do you need to implement a destructor?

1. Write an algorithm to detect a cycle in a linked list.

2. Remove consecutive duplicate entries from a given linked list.

3. Get the k-th to the last element in the linked list (when the size of the list is not known/tracked).

4. Check if the linked list is a palindrome.

5. Reverse a singly-linked list non-recursively.

1. Design a stack such that getMinimum() is O(1).

2. Reverse the elements of stack only using stack operations.

1. Write a recursive algorithm to perform x^n.

2. Write a recursive algorithm to count the number of 1’s in a given binary representation.

1. Given a binary tree, print the elements in a new order, as suggested by the example shown below:

The expected output is: 10, 14, 2, 12, 25, 75, 8, 50, 15.

2. Find the nearest match of a given value in a binary search tree.

3. Write a function to check whether two binary trees are structurally identical.

4. Write a function to find the size of a BST.

5. Write a function to compute the sum of all elements in a BST.

Big-oh notation

Big-omega notation

Big-theta notation

Solving recurrence equations

**Problems**

1. For the sorting algorithm below, compare the time complexity for partitioning in the middle vs in the beginning.

List L; Sorted List L’; Size = n Recursive function: SORT(L) Basis: (n == 1) => L’ = L Inductive: (n > 1) => 1. Split L into two non-empty sets L1 and L2. 2. L1’ = SORT(L1); L2’ = SORT(L2) 3. L’ = COMBINE(L1’, L2’)

2. Write down the lower bound, upper bound and order functions for f(n) = 0.5 n^3 + 2 n^2 + log n

3. For the following snippet of code, analyze the worst case complexity for the following code.

for (int i = 0; i < n; ++i) { for (int j = i+1; j < n; ++j) { if (A[i] == A[j]) { return true; } } return false; }

4. Solve the recursive equation: T(1) = 1; T(n) = T(n-1) + n(n-1), n>1.

5. What is the min number of comparisons needed to find both max and min of n integers?

6. What is the min number of comparisons to find the max and the second max of n integers?

Insertion sort – algorithm, complexity and benefits

Selection sort – algorithm, complexity and benefits

Quicksort – algorithm, complexity and benefits

Mergesort – algorithm complexity and benefits

**Problems**:

- Discuss the sorting algorithms covered in the class.
- Discuss the comparison of computational complexities of these sorting algorithms.
- Given an array of n numbers, give an algorithm for checking whether there are repeated elements, without any additional space overhead.
- Given an array consisting of 0’s, 1’s and 2’s, give an algorithm for sorting the array.
- Given an array of 1M pixel color values and each of which is an integer in the range [0,255], which sorting algorithm is preferable for such an array?

Splay operations

- Zig-zag
- Zig-zig
- Zig

Complexity of operations in splay trees

Algorithms for find(key), min(), max(), remove(key)

**Problems**:

- Algorithms for inserting, deleting and finding a key in a splay tree?
- Compare splay tree with binary search tree.

Relationships between priority queues, selection sort, and the heap data structure.

Operations: min(), insert(key), removeMin(), bottomupHeap(), heapsort()

In-place heap sort

**Problems**:

- What are the minimum and maximum number of elements in a heap of height h?
- Given a min-heap, how would you find the max element? What is the complexity of such an algorithm?
- Insert the following numbers into a heap for an in-place sort in descending order: 3, 5, 2, 9, 20
- Algorithms for inserting and deleting an entry in a heap?
- Given a big file containing billions of numbers, how do we find the max 10 numbers?

Representations: Adjacency matrix, Adjacency list

Graph traversals (DFS, BFS)

Shortest path algorithm

**Problems**:

- Give an algorithm for checking whether a given graph has a simple path from a source node (s) to a destination node (t).
- Assuming an adjacency matrix representation of a graph, count all the simple paths from source (s) to destination (t).
- Given a directed graph, give an algorithm to detect whether there is a cycle.
- Given an undirected graph, give an algorithm to detect whether there is a cycle.
- Given a directed acyclic graph, give an algorithm to topologically sort the nodes.
- Find the shortest graph distances between every pair of vertices in a given graph. Assume there the graph only has non-negative weights on the edges.

Hashcoding and compression function

Examples of bad and good hash-coding

Examples of bad and good compression function

Collision, load-factor

**Problems**:

- Give an efficient algorithm to find the first non-repeated character in a string.
- Give an efficient algorithm to find the first repeated character in a string.
- Given an array of n numbers, present an algorithm which displays all pairs whose sum is S.
- Given two sets A and B, and a number K, present an algorithm for finding whether there exists a pair of elements, one from A and one from B, that add up to K.
- How would you implement an ordered/unordered set/map?

]]>

To provide the password, please first visit this page over https:

]]>To provide the password, please first visit this page over https:

https://sites.tufts.edu/comp15/feed

]]>Generalize your solution to take N as input, and produce the above pattern of any size. N must be a power of 2 to generate a valid pattern.

The above snapshot is taken with N = 8. With N=4, the output would be as follows:

**Problem 2**: Given a binary search tree, print the elements in a *new* order, as suggested by the example shown below:

The expected output is: 10, 14, 2, 12, 25, 75, 8, 50, 15.

**Sample output**:

Printing the tree in reverse level order:

10 14 2 12 25 75 8 50 15

Dotty output in tree_output.dot:

digraph{

15 -> 8

8 -> 2

NULL0[shape=point]

2 -> NULL0

NULL1[shape=point]

2 -> NULL1

8 -> 12

12 -> 10

NULL2[shape=point]

10 -> NULL2

NULL3[shape=point]

10 -> NULL3

12 -> 14

NULL4[shape=point]

14 -> NULL4

NULL5[shape=point]

14 -> NULL5

15 -> 50

50 -> 25

NULL6[shape=point]

25 -> NULL6

NULL7[shape=point]

25 -> NULL7

50 -> 75

NULL8[shape=point]

75 -> NULL8

NULL9[shape=point]

75 -> NULL9

}

Copy and paste the above output into the dotty layout tool and click on “Make me a graph” in order to visualize the graph.

Alternatively, you could also execute “dotty tree_output.dot” from your unix shell, provided you have the native DISPLAY support.

]]>1. Write a recursive algorithm to perform x^n.

2. Write a recursive algorithm for binary search.

]]>1. Write an algorithm to detect a cycle in a linked list.

2. Remove consecutive duplicate entries from a given linked list.

3. Get the k-th to the last element in the linked list (when the size of the list is not known/tracked).

4. Check if the linked list is a palindrome.

5. Reverse a singly-linked list non-recursively.

**Stacks/Queues**:

1. Design a stack such that getMinimum() is O(1).

2. Reverse the elements of stack only using stack operations.

]]>**Link to source code**: http://www.cs.tufts.edu/comp/15/examples/queues/queue.h

#include "sll.h" /// A queue (FIFO) implementation using singly linked list class queue { public: /// Don't need to overwrite the default constructor, destructor, /// copy constructor or assignment operator. /// Push aValue into the back of the queue void enqueue(int aValue) { mLL.addBack(aValue); } /// Pop out the value from the front of the queue void dequeue() { if (isEmpty()) { cout << "Queue is empty. Cannot dequeue!" << endl; return; } mLL.removeFront(); } /// Fetch the front value from the queue (without deleting it) int front() { return mLL.front(); } /// Check whether the queue is empty bool isEmpty() { return mLL.isEmpty(); } private: SLinkedList mLL; };]]>

**Link to source code**: http://www.cs.tufts.edu/comp/15/examples/stacks/stack.h

#include "sll.h" /// A stack implementation using singly linked list class stack { public: /// Don't need to overwrite the default constructor, destructor, /// copy constructor or assignment operator. /// Push aValue into the stack void push(int aValue) { mLL.addFront(aValue); } /// Pop out the top value from the stack void pop() { if (isEmpty()) { cout << "Stack is empty. Cannot pop!" << endl; return; } mLL.removeFront(); } /// Fetch the top value from the stack (without deleting it) int top() { return mLL.front(); } /// Check whether the stack is empty bool isEmpty() { return mLL.isEmpty(); } private: SLinkedList mLL; };]]>

**Link to source code**: http://www.cs.tufts.edu/comp/15/examples/linked_lists/sll.h

#include <iostream> #include <cassert> using namespace std; /// Singly Linked List with a head pointer, a tail pointer and size class SLinkedList { private: class SLLNode; public: /// Constructor SLinkedList() : mHead(NULL), mTail(NULL), mSize(0) { } /// Insert a node in the front of the list void addFront(int aValue) { mHead = new SLLNode(aValue, mHead); if (mTail == NULL) { // First entry mTail = mHead; } mSize++; } /// Insert a node in the back of the list void addBack(int aValue) { SLLNode* node = new SLLNode(aValue); if (mHead == NULL) { mHead = mTail = node; } else { mTail->setNext(node); mTail = node; } mSize++; } /// Delete the node from the front void removeFront() { assert((mHead == NULL && mTail == NULL) || mSize > 0); if (mHead == NULL) { return; } SLLNode *oldHead = mHead; mHead = mHead->next(); if (mHead == NULL) { mTail = mHead; } delete oldHead; mSize--; } /// Delete everything void removeAll() { while(mSize > 0) { removeFront(); } mHead = mTail = NULL; mSize = 0; } /// Get the value from the front int front() const { if (isEmpty()) { cout << "list empty." << endl; return -1; } return mHead->value(); } /// Get the value from the back int back() const { if (isEmpty()) { cout << "list empty." << endl; return -1; } return mTail->value(); } /// Check whether the list is empty bool isEmpty() const { return (size() == 0); } /// Get the size of the list int size() const { return mSize; } /// Get the n-th value from the list int nth(int n) const { if (mSize == 0) { cout << "list empty." << endl; return -1; } if (n >= mSize) { cout << "list size smaller than the index." << endl; return -1; } SLLNode *nthNode = mHead->nth(n); assert(nthNode != NULL); return nthNode->value(); } /// Print list void output(ostream& s = cout) const { s << "List:" << endl; if (mSize == 0) { s << "Empty!" << endl; return; } SLLNode *node = mHead; for (int i = 0; i < mSize; ++i) { s << node->value() << " "; node = node->next(); } s << endl; } /// Copy constructor SLinkedList(const SLinkedList& other) : mHead(NULL), mTail(NULL), mSize(0) { if (!other.isEmpty()) { // Copy from other for (const SLLNode* curr = other.head(); curr != NULL; curr = curr->next()) { addBack(curr->value()); } } } /// Assignment operator SLinkedList& operator=(const SLinkedList& other) { if (this == &other) { // self-assignment -- return otherwise bad things will happen return *this; } // Cleanup this list removeAll(); // Copy from other if (!other.isEmpty()) { for (const SLLNode* curr = other.head(); curr != NULL; curr = curr->next()) { addBack(curr->value()); } } return *this; } /// Destructor ~SLinkedList() { removeAll(); } private: /// Subclassing so that SLLNode cannot be instantiated publicly class SLLNode { public: /// Constructors SLLNode(int aValue) : mValue(aValue),mNext(NULL) { } SLLNode(int aValue, SLLNode *aNext) : mValue(aValue), mNext(aNext) { } /// Get value int value() const { return mValue; } /// Get next node for const node receiver const SLLNode *next() const { return mNext; } /// Get next node for non-const node receiver SLLNode *next() { return mNext; } /// Set next node void setNext(SLLNode* aNext) { mNext = aNext; } /// Return the n-th node w.r.t. this node SLLNode* nth(int n) { if (n == 0) { return this; } else if (n < 0 || mNext == NULL) { return NULL; } else { return mNext->nth(n-1); } } /// Insert a new node with aValue after this node void insertAfter(int aValue) { mNext = new SLLNode(aValue, mNext); } /// Delete after this node void deleteAfter() { if (mNext == NULL) { return; } SLLNode *node = mNext; mNext = node->next(); delete node; } private: int mValue; SLLNode* mNext; }; SLLNode* mHead; SLLNode* mTail; int mSize; }; /// Overloaded operator<< for outputing a list ostream& operator<<(ostream& s, const SLinkedList& aList) { aList.output(s); return s; }]]>

**Data structure**: A method to store information associated with the algorithm.

Programs = Algorithms + Data structures -Niklaus Wirth

]]>