ghostbody / 15-cpp-learning

15 c++ learning repository of SDCS SYSU
6 stars 3 forks source link

Tencent Internship Interview Questions Summary #4

Open ghostbody opened 8 years ago

ghostbody commented 8 years ago

I am very luck to have two chance for a front-end interview and a back-end interview.

Front-end interview

Question: Give you an "a" element, and the link is broken, how can you disable the link? Answer:

  1. Write a js ajax script to test whether the link is good or not, then with a callback, you can disable it.
  2. Deal with the request in back-end, redirect the page to other place.
  3. Make another element over the a element or set its css to "display:none"
  4. Using event listening, disable the basic page response message.

Question: How can you get elements from a document? Answer:

  1. Using getElementById().
  2. Using JQuery, query string. ......

Question: Other methods? Answer: Get the whole document, and then traversal it......

Question:........

Question: How to traversal it? Answer: Using a stack, put the root node into it, and then loop until the stack is empty. For each iteration, get the top of the stack, and then push the child nodes of the node into the stack again.

Question: What kind of this traversal? Answer: Deep first traversal.

Question: How can you implement breadth first traversal? Answer: Using a queue rather than a stack.

Question: What's the 5 level of TCP/IP protocol? Answer: Application, Transmission, Networking, data link layer and physical layer.

Question: What protocol does transmission layer have? Answer: TCP and UDP

Question: Tell their difference? Answer: TCP is an RDT (reliable data transmission) protocol and it provides traffic control and congestion avoidance. It's a linked based protocol, peers should linked to each other before data transport. HTTP, FTP base on TCP. UDP is a connectionless protocol and it does not provide reliable data transmission or traffic control or congestion avoidance. But it can get a big througoutput in practice. You should implement reliable data transmission if you need it in application layer if you use this protocol, for example QQ protocol. RDP, DHCP base on UDP.

Back-end interview

Question: What language you are most familiar with? Answer: C++

Question: What's the efficiency of the method push_back() in vector and list? Compare it. Answer: In the condition that the storage of vector is not full, vector is faster than list. But in the condition that the storage is full, list is faster because vector need to allocate new storage and it's very slow.

Question: What's the value of "sizeof(object)" where object is an class instance? Answer: If the class is empty or only with member functions, the value is 1. Otherwise the size should be the sum of the size of it's members and also memory alignment will be applied when you are calculating the size of a class.

Question: Does the order of the member variables matter the value of the sizeof(object)? Answer: Nope.

Question: Give you a c string, say an char array 'char s1[100]' and a char point char * s2 = "bacse", what's the value of sizeof() for them respectively? Answer: sizeof(s1) is 100. And in 32 bits operating system s2 is 4, while s2 is 8 in 64 bits operation system.

Question: We have a condition that there is a class with so many member variables, how can you solve if I want the user of this class to create instance only in the heap rather than in the stack? (我现在有一个类,里面有非常多的成员变量,我现在希望用户在实例化的时候不占用栈的空间,迫使他使用堆的内存,我应该怎么做?) Answer: You can let the constructor of the class private as well as the assign operator like you are using a singleton and then provide a public function "static object * getInstance()" for the user to get instance from the heap. This answer is my answer, I think it for a long time when I am on my way back. It's a trick!!!!! In fact, some of thr the objects are stored in the heap, they only store pointers in the stack!!!

Question: What is memory leak? How to solve it? Answer: When you allocated memory from the heap using malloc or new, you forget to release it using delete or free, or you make a mistake when you have pointer operations, and a block of memory will be lost. This is memory leak.

Question: How to solve this problem? Or how to avoid this problem? Answer: For single thread program, you need to pay attention when you using new or malloc. You should have paired operation , new with delete, malloc with free. Also you can use some software like valgrind to check you memory. Also you can overload new and delete operator to manage the memory. In multi-threading program, you should have some contract with the functions, specified the usage of each role. Never return a block of memory in a function.

Question: What's the difference of fgets() in C standard library and read() system call in linux? Answer: ........ I get it on my way back, fgets() is thread safe and read() is not (reader-writer problem in process). sad....

Question: What's inter process communication methods? Answer: Message queue, shared Memory and ......socket.

Question: What's the disadvantage of using socket as communication methods? Answer: It will pass lo(本地回环) and it will pass the TCP/IP stack. It will cause protocol overhead. And also it need to occupy some ports.

Question: How can you see all the occupying ports? (He means linux) Answer: Using command "sudo nestat"

Question: Dose kernel thread allocate memory for malloc() when it uses system call? Answer: ....... (MengBing)

Nope, you just need to switch to kernel mode and then allocate memory and then switch back to user

Ok, your knowledge is just OK. C++ is good but operating system is poor.

KatharinaLin commented 8 years ago

Using English for the interview?I'm poor at the theorical knowledge.Can +7 TA give me some advice?And I always debug these days.Sometimes I'm so sad for debugging.

ghostbody commented 8 years ago

@KatharinaLin nope...Chinese interview. Have a deep learn for c++. Patient...Patient... and Patient

DaddyTrap commented 8 years ago

Oh...Reading this passage, I am thinking "Interview is interesting, isn't it ??". Maybe it is just one who hasn't had an interview that think it interesting......

It cost me some time to understand the English words......However, at least I learned something about memory alignment. And......It seems that we can't learn only C++, but also many things about the operation system.


May I ask a question?

Question: We have a condition that there is a class with so many member variables, how can you solve if I want the user of this class to create instance only in the heap rather than the stack? (我现在有一个类,里面有非常多的成员变量,我现在希望用户在实例化的时候不占用栈的空间,迫使他使用堆的内存,我应该怎么做?) Answer: You can let the constructor of the class private as well as the assign operator like you are using a singleton and then provide a public function "getInstance()" for the user to get instance from the heap. This answer is my answer, I think it for a long time when I am on my way back. It's a trick!!!!! In fact, all the objects are stored in the heap, the compile only stores a pointer like variable in the stack!!!

Here you say this is your answer and it is a trick. So.....is the answer really true, or just a trick ?

ghostbody commented 8 years ago

@DaddyTrap Well, I do not know the exactly answer, maybe I am right. But wan hai fa shi told me that all objects are stored in the heap although it looks like in the stack, for example:

list<int> li;
vector<int> vec;

The example class mange memory in the heap; Also,


class A {
int a;
};

A a;

I do not know a is in stack or heap?

In stack, I wonder.

JohnTargaryen commented 8 years ago

新建文本文档.txt hello, +7. i've met a problem in "JobManager". in the TXT that i uploaded, there are two block of codes, but only the upper block can function while the one below somehow occurs memory error. I tried to debug it with gdb, only to find that scope "Job next(priority);" uses the same address to construct Job all the time and as a result the pointers keep pointing to themselves. would you be kind enough to explain why this happen? THX.

ghostbody commented 8 years ago

@JohnTargaryen Well, may you paste you code here using mardown? It's easy.

image

int main() {
   return 0;
}
JohnTargaryen commented 8 years ago
void JobManager::addJob(int priority) {
    Job* temp = jobFrontPointer;
    if (jobFrontPointer == NULL) {
        jobFrontPointer = new Job(priority);
        return;
    }
    for (int k = 1; k <= getNumOfJob() - 1; k++)
    temp = temp->getNext();
    temp->setNext(new Job(priority));
}

void JobManager::addJob(int priority) {
    Job next(priority);
    if (jobFrontPointer == NULL) {
        jobFrontPointer(&next);
        return;
    }
    for (int k = 1; k <= getNumOfJob() - 1; k++)
    temp = temp->getNext();
    temp->setNext(&next);
}

dont know if this will work or not...

ghostbody commented 8 years ago

Well, it's a basic question in c++. The first block of code works because it get memory from the heap. The second block fails because it get memory from stack. When the function "addJob" is end, all the contents in the stack will be destroy including the Job next(priority). So when the function is end, the address &next is invalid. If you read it, memory error will reported. Also, GDB will show weird info to you.

Hope it helps you.

JohnTargaryen commented 8 years ago

it does help a lot, thanks for your time!

KatharinaLin commented 8 years ago

Wow,github is great,I can view it from my email……

ghostbody commented 8 years ago

@KatharinaLin Well, you can discover more interesting things as you explores.:wink::wink::wink:

DaddyTrap commented 8 years ago

@ghostbody I test in Windows, using MinGW as the compiler. I don't know if the test code is right, but maybe I get some different answer....

My code is following.

#include <iostream>
#include <vector>

using namespace std;
class A {
  int a;

 public:
  A() {}
  ~A() {}
};
int main() {
  int a;
  A b;
  std::vector<char> v;
  int *aa = new int;
  A *bb = new A();
  std::vector<char> *vv = new std::vector<char>;
  cout << "&a is: " << &a << endl;
  cout << "&b is: " << &b << endl;
  cout << "&v is: " << &v << endl;
  cout << "aa is: " << aa << endl;
  cout << "bb is: " << bb << endl;
  cout << "vv is: " << vv << endl;

  return 0;
}

Running this I get the following output.

&a is: 0x28ff00
&b is: 0x28fefc
&v is: 0x28fef0
aa is: 0x6d1ef8
bb is: 0x6d1f28
vv is: 0x6d1790

Seems that their allocation is what they "look like"....

ghostbody commented 8 years ago

@DaddyTrap Yes, your job is exactly right. But what I am wondering is that, for example &b is: 0x28fefc, but what about its member vriables? Are they store in 0x28fefc+ ?

The answer is 'Yes', compiler gcc 5.2.1, linux

class A {
  int a;
public:
  int * get() {
    return &a;
  } 
};

int main() {
  A a;
  printf("%p\n%p\n", &a, a.get());
  return 0;
}

output:

0x7ffcbe04bcb0
0x7ffcbe04bcb0

And also, as we all know, list uses dynamic allocation for node appending. And vector uses dynamic array. All of them are in the heap.


Maybe my answer is correct....

#include <stdio.h>

class A {
private:
  A() {}
  A(const A & another) {}
  A& operator=(const A & another) {}
public:
  static A * newInstance() {
    return new A();
  } 
  static bool deleteInstance(A * instance) {
    if(instance != NULL) {
      delete instance;
      return true;
    }
    return false;
  }
};

int main() {
  A * a = A::newInstance();
  A::deleteInstance(a);
  a = NULL;
  return 0;
}
DaddyTrap commented 8 years ago

@ghostbody Oh! I got it! THX for your patient answering!

KatharinaLin commented 8 years ago

So member variable is different from the normal variable.And the member variable is exactly in heap?

ghostbody commented 8 years ago

@KatharinaLin Nope, it's just the opposite. Member variables are also stored in stack with statement A a.

KatharinaLin commented 8 years ago

I don't understand.So what is in the heap?

ghostbody commented 8 years ago

@KatharinaLin Heap: This area is management by the operating system and it's shared by other programs. You can get dynamic memory allocations in this area. image