Coding Interviews 1

The first day. chapter 1&2 Open-mouthed smile

Implement assignment function for below class CMyString.

class CMyString
{
public:
    CMyString(char* pData = NULL);
    CMyString(const CMyString& str);
    ~CMyString(void);
private:
    char* m_pData;
};

Before coding I should focus on four points:

1 Whether the type of return value is the reference of CMyString, and returning the object itself(*this). If only return a reference, I can assign continuously like str1=str2=str3. Otherwise, the codes cannot be compiled.

2 If the parameter is not const CMyString&, from formal parameter to actual parameter the constructer will be invoked. Reference will not cause this kind of waste and increase the efficiency. const can help you not change the parameter for safety.

3 Before get a new chunk of memory, I should release the existing memory first, in case of memory leak.

4 Make a judgment on whether the parameter is the same as (*this). If they are same, then return *this. If don’t make a judgment, you will release the memory of parameter too. You will not find the source of assignment.

CMyString& CMyString::operator=(const CMyString& str)
{
    if (this == &str)
        return *this;
    delete[] m_pData;
    m_pData = NULL;

    m_pData = new char[strlen(str.m_pData) + 1];
    strcpy(m_pData, str.m_pData);
    return *this;
}

more advanced code

CMyString& CMyString::operator=(const CMyString& str)
{
    if (this == &str)
        return *this;
    else
    {
        CMyString tmp(str);
        char* ctmp = m_pData;
        m_pData = tmp.m_pData;
        tmp.m_pData = ctmp;
    }
    return *this;
}

In this code, we consider about the problem on new. If the memory is not enough, then before we change the content of original object. The exception bad_alloc will be thrown out.

转载于:https://www.cnblogs.com/Bogart/p/4809195.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The first chapter focuses on the interview process. A typical interview process can be divided into two phases: phone interviews (including phone-screen interviews) and on-site interviews. Usually there are three steps in each round of interview, which are the behavioral interview, technical interview, and general Q/A. Tips are provided for each stage of interviews. The next three chapters cover basic programming knowledge. Technical interview questions on four popular programming languages (C, C++, C#, and Java) are discussed in Chapter 2. The most common data structures (including arrays, strings, lists, trees, stacks, and queues) and algorithms (including search, sort, backtracking, dynamic programming, greedy algorithms, and bit operations) are discussed in Chapter 3 and Chapter 4 respectively. Chapter 5 discusses three factors of high quality code. Interviewers usually expect candidates’ code to fulfill the functional requirements as well as cover corner cases and handle invalid inputs gracefully. After reading this chapter, you should get the idea so that you will write clear, complete, and robust code. Three strategies to solve difficult coding interview problems are provided in Chapter 6. If hard problems are met during interviews, candidates should figure out solutions before they write code. After reading this chapter, you may get three strategies to solve problems: figures to visualize problems, step-by-step analysis on examples to simplify problems, and divide-and-conquer strategies to break complex problems into manageable pieces. The topic of Chapter 7 is performance optimization. If there are multiple solutions to a problem, usually interviewers expect the most efficient one. The strategies to improve time efficiency and make trade-off decisions between time and space are discussed with several sample coding interview questions. Chapter 8 summarizes various skills for interviews. Interviewers usually pay close attention to candidates’ communication and learning skills. Additionally, many interviewers like to examine candidates’ skills of reapplying knowledge, mathematical modeling, and divergent thinking. Chapter 9 closes this book with two interview cases, which highlight good behavior expected by interviewers and the most common mistakes made by candidates.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值