9. Concentrix
Role |
Senior Software Engineer |
Profile |
C++, Payments, Design Patterns |
9.1. Round 1
Date |
2024-02-05T16:00:31+0530 |
// Round 1 - 2=024-02-05T16:00:31+0530
// struct c
typedef struct node {
int data1;
int data2;
char data3;
} node_t;
int main(int ac, char &av)
int main(int ac, char **av)
{
node_t *n = (node_t *)malloc(5 * sizeof(node_t));
n[0]->data = 5;
printf("%d\n", n->data);
n = (node_t *)realloc(10 * sizeof(node_t));
free(n);
return 0;
}
// memory
/*
High->program args->stack->free meory |
stack pointer meets heap pointer->heap
->bss
segment(uninitlized global &static variables)
->data segment(initilized global &static variables)
->read,
read - write areas low->code.text
*/
// cpp encapsulation & polymorphism
class Node
{ // encapsulation
private:
int data;
public:
void set_data(int a);
int get_data(void);
}
template <typename T>
class Node
{ // compile time polymorphism (template, function overloading) + runtime polymorphism (virtual functions)
private:
T data;
public:
void set_data(T a);
T get_data(void);
}
// allocate dynamic memory in cpp
Node *n = new Node[10];
delete[] n;
// copy constructor
class Node
{
private:
int data;
public:
Node() : {};
Node(const Node &node) : data(node.data){}; // Node n1; Node n2(n1);
operator=(const Node &other) { data = other.data; } // Node n1 = n2;
void set_data(int a);
int get_data(void);
}
// type casting: static_cast, dynamic_cast, const_cast & reintepret_cast
void *ptr = new Node();
Node *n = dynamic_cast<Node *>(ptr);
// cpp
vector<int> nums{1, 2, 3}; // c++11
vector nums{1, 2, 3}; // c++17
// Design patterns: singleton, state, builder, observer
enum NodeStates_e {
start,
run,
end
}
class NodeState
{
prviate : NodeState_e state;
NodeState curr;
public:
void set_state();
NodeState *get_state()
{
switch (state) {
case start:
if (!curr) return new NodeStartState() return curr;
}
}
}
class NodeStartState : NodeState
{
}
// cpp: multi threading
int thread_func(void *arg)
{
while (1) {
print("", );
sleep(2);
}
}
int data = 5;
std::thread t1(thread_func, *data);
std::thread t2(thread_func, *data);
t1.join();
t2.join();
// cpp networking protocols: tcp/ip, http, sip
9.2. Round 2
Date |
2024-02-06T15:30:31+0530 |
// Concentrix C++ round 2
// Singleton pattern
class Test
{
public:
static int num;
int &rnum;
int *pnum;
static Test *getObject(void)
{
// use mutex to gaurd
if (!this_obj) {
this_obj = new Test; // check if already created
}
return this_obj;
}
private:
Test *this_obj;
Test() : rnum(0), pnum(0) {}
Test(const Test &t);
Test operator=(const Test &t);
}
int Test::num = 0;
int main(int ac, char **av)
{
std::thread t1, t2;
Test obj1 = Test::getObject();
return 0;
}
// map
int main(int ac, char **av)
{
std::thread t1, t2;
Test obj1 = Test::getObject();
return 0;
}
class TestDerived : public Test
{
}
class TestDer2;
// cast operators
int main(int ac, char **av)
{
char b = 10;
int a = static_cast<int>(b);
Test *ptr = new TestDerived();
TestDerived *ptr2 = dynamic_cast<TestDerived>(ptr); // no error
TestDerived *ptr3 = dynamic_cast<TestDer2>(ptr); // error
const char cb = 10;
char b1 = const_cast<char>(cb);
TestDerived *ptr3 = reinterpret_cast<TestDer2>(ptr); // no error
return 0;
}
// function override
class TestBase
{
public:
int num;
virtual int add(int a) { return num + a; }
vritual ~TestBase()
}
class TestDerived : public TestBase
{
public:
int add(int a) override { return num + a + 2; }
}
// object overload
class TestBase
{
public:
virtual int operator()(int a) { return num + a; }
}
// vptr