Goldman Sachs Interview Question
Software Engineer / Developers1) ++a means increment & use. a++ means use & increment. i.e use the same value and then increment. This means a temporary is created to hold & use the same value. While a is incremented. So, ++a is faster & it is always better practice to use ++a
2) A constructor a) with one parameter or b) which has at least one default parameter can do implicit conversions
A) MyClass1( int num );
B) MyClass1( const MyClass1 &irOther);
MyClass1 ob1 = 15; // convert 15 to an instance of MyClass1 using 1st then call copy constructor i.e 2nd one to make this call succeed.
C) MyComplexNumber( double iReal, double iImaginary = 0);
D) MyComplexNumber( MyComplexNumber );
double realval = 15;
MyComplexNumber obj2 = realval; // we do not specify any impaginary here. Compiler converts realval to an object of type MyComplexNumber using C). Then calls D) to create obj2
Its a good practice to use the keyword 'explicit' with your constructors so that such implicity conversions can be checked.
A) explicit MyClass1( int num );
C) explicit MyComplexNumber( double iReal, double iImaginary = 0);
3) String::copy works on only string types. For memcopy the underlying type is irrelevant. It is a binary copy of the data.
4) Global & static variables
5) F2() has to assume that x has been newed i.e F1() has been called. This results in dependency of F2() on F1(). The design is flawed. If F1 throws an exception, F2 may not be called & memory for x cannot be reclaimed as long as the program is running.
6) Copy by value. There is no alternative
A) i + j;
B) k = i + j;
This calls operator + (a, b). You do not want to use the value of i as the final result. i should remain as it is !!
You have to create a temporary object from i (or j) using copy constructor. Add the data member values of j to the data member of temp. Return temp.
Note- It is always a better practiceto implement non-member function operator + (a,b) in terms of class member function operator += (b) for localiziing the core logic.
Just use gcc -S to generate assembly code for ++a and a++ on Red Hat Linux. I found the generated the same assembly instruction. So, I think it is compiler dependent, which one is faster because on my Linux machine, it is the same.
1) ++a is faster.
- vodangkhoa March 10, 20072) a constructor with one argument is an implicit constructor.
4) Things that run before main enter would be Static Variable Initializations. Check on that.
5) F2() might through an exception and delete x might never be called
6)copy