Notes on Technical Interview Prep

Posted by Siya Li on 2018-10-18

Note: WIP

Problems

Q1 - p24 - write a function for the assignment operator on class CMyString

(1) The return type of this function should be a pointer to CMyString, i.e., return *this;
(2) To improve efficiency, the formal parameter should be a constant reference instead of a class instance
(3) Free up the current space (otherwise memory leak)
(4) Check equality (avoid freeing up the space of the copied instance)
(5) [Optimized] Exceptional safety

Q2 - p31 - design a class Singleton which only allows one instance

(1) [Restricted to single thread]A private constructor + a static instance
(2) [Inefficient under multi-thread] Init/add a lock before checking null
(3) [Improved] Wrap the current lock with another check for null instance
(4) [Semi-optm but C#-specific] A public static constructor
(5) [Optimized but C#-specific] Embed in another nested class only called upon Singleton5.Instance

Q3 - p38 - look up a value in a two-d array with the rule left < right, top < bottom

(1) [Key Idea] Exclude one column/row at each comparison
(2) [Key Idea] For each comparison, start at either the upper right corner or the lower left one.
(3) [Hint] To access the two-d array, from matrix[], use an index of row*columns + column

Q4 -

Summary

p24 - C++

s1. Why compiler does not allow parameters in the copy constructor to be passed by value
[infinite loop and stack overflow]

p28 - C#

s2. What is the difference between struct and class in C++
[struct/public vs class/private for member functions and variables]

p37 - Data Structure

s3. How does vector in C++ standard library work
[copy previous data to a new array of twice as the size before; free up the old array]