Posts

Showing posts with the label c

C/C++ Interview Question&Answer Part 1

Image
How do you find out if a linked-list has an end?  (i.e. the list is not a cycle) You can find out by using 2 pointers. One of them goes 2 nodes each time. The second one goes at 1 nodes each time. If there is a cycle, the one that goes 2 nodes each time will eventually meet the one that goes slower. If that is the case, then you will know the linked-list is a cycle. What is the difference between realloc() and free()? The free subroutine frees a block of memory previously allocated by the malloc subroutine. Undefined results occur if the Pointer parameter is not a valid pointer. If the Pointer parameter is a null value, no action will occur. The realloc subroutine changes the size of the block of memory pointed to by the Pointer parameter to the number of bytes specified by the Size parameter and returns a new pointer to the block. The pointer specified by the Pointer parameter must have been created with the malloc, calloc, or realloc subroutines and not been deallocated with...

C/C++ Interview Question&Answer Part 2

Image
What is the difference between an object and a class? Classes and objects are separate but related concepts. Every object belongs to a class and every class contains one or more related objects. - A Class is static. All of the attributes of a class are fixed before, during, and after the execution of a program. The attributes of a class don't change. - The class to which an object belongs is also (usually) static. If a particular object belongs to a certain class at the time that it is created then it almost certainly will still belong to that class right up until the time that it is destroyed. - An Object on the other hand has a limited lifespan. Objects are created and eventually destroyed. Also during that lifetime, the attributes of the object may undergo significant change. Suppose that data is an array of 1000 integers. Write a single function call that will sort the 100 elements data [222] through data [321]. quicksort ((data + 222), 100); What is a class? Class is a us...

C/C++ Interview Question&Answer Part 3

Image
How does throwing and catching exceptions differ from using setjmp and longjmp? The throw operation calls the destructors for automatic objects instantiated since entry to the try block. What is a default constructor? Default constructor WITH arguments class B { public: B (int m = 0) : n (m) {} int n; }; int main(int argc, char *argv[]) { B b; return 0; } What is a conversion constructor? A constructor that accepts one argument of a different type. What is the difference between a copy constructor and an overloaded assignment operator? A copy constructor constructs a new object by using the content of the argument object. An overloaded assignment operator assigns the contents of an existing object to another existing object of the same class. When should you use multiple inheritance? There are three acceptable answers: "Never," "Rarely," and "When the problem domain cannot be accurately modeled any other way." Explain the ISA and HASA class relationsh...

K-th Node from End - Interview Question&Answer

Image
Problem:  Get the K th  node from end of a linked list. It counts from 1 here, so the 1 st  node from end is the tail of list.  For instance, given a linked list with 6 nodes, whose value are 1, 2, 3, 4, 5, 6, its 3 rd  node from end is the node with value 4. A node in the list is defined as: struct  ListNode {      int        m_nValue;     ListNode* m_pNext; }; Analysis:  In a list with n nodes, its k th  node from end should be the (n-k+1) th  node from its head. Therefore, if we know the number of nodes n in a list, we can get the required node with n-k+1 steps from its head. How to get the number n? It is easy if we scan the whole list from beginning to end. The solution above needs to scan a list twice: We get the total number of nodes with the first scan, and reach the k th  node from end with the second scan. Unfortunately, interview...

Numbers with a Given Sum - Interview Question&Answer

Image
Problem 1:   Given an increasingly sorted array and a number s, please find two numbers whose sum is s. If there are multiple pairs with sum s, just output any one of them. For example, if the inputs are an array  { 1 ,  2 ,  4 ,  7 ,  11 ,  15 } and a number 15, please out two numbers 4 and 11 since 4+11=15. Analysis:  Let us firstly have a try to select two numbers (denoted as num1 and num2) of the input array. If their sum equals to s, we are fortunate because the required numbers have been found. If the sum is less than s, we may replace num1 with its next number, because the array is increasingly sorted and its next number should be greater. If the sum is greater than s, num2 can be replaced with its previous number in the sorted array, which should be less than num2. Take the array  { 1 ,  2 ,  4 ,  7 ,  11 ,  15 } and number 15 as a...