Outscal
Tech Mahindra Interview Questions and Answers
Q1. What is Diamond Problem in C++ ? How to solve it ?
Diamond Problem occurs in multiple inheritance when a class inherits from two classes that have a common base class.
Diamond Problem arises when a class inherits from two classes that have a common base class.
Ambiguity arises in accessing the common base class members.
To solve Diamond Problem, virtual inheritance is used to ensure only one copy of the common base class is inherited.
Q2. Contructors and its types. Types of Copy Constructor
Constructors are special member functions in a class used to initialize objects. Copy constructors create a new object as a copy of an existing object.
Types of constructors: Default constructor, Parameterized constructor, Copy constructor, and Destructor
Types of copy constructors: Shallow copy constructor and Deep copy constructor
Shallow copy constructor copies the values of the members of one object to another object. Example: MyClass(const MyClass &obj) { x = obj.x; y = obj...read more
Q3. Virtual Functions and why is it needed ?
Virtual functions in C++ allow for dynamic polymorphism by enabling a function to be overridden in a derived class.
Virtual functions are used in C++ to achieve runtime polymorphism, where the function to be called is determined at runtime based on the object's type.
They are needed to enable a base class pointer to call a function that is overridden in a derived class.
Virtual functions help in achieving the Open/Closed principle of object-oriented programming, allowing for ext...read more
Q4. What are Smart Pointers
Smart pointers are objects that manage the memory of dynamically allocated objects in C++ to prevent memory leaks.
Smart pointers automatically delete the object they point to when they are no longer needed.
Examples include unique_ptr, shared_ptr, and weak_ptr in C++.
They help in preventing memory leaks and dangling pointers.
Smart pointers provide better memory management compared to raw pointers.
Q5. Middle of a linked list
To find the middle of a linked list, use two pointers - one moving at double the speed of the other.
Initialize two pointers, slow and fast, both pointing to the head of the linked list.
Move the slow pointer by one node and the fast pointer by two nodes in each iteration.
When the fast pointer reaches the end of the list, the slow pointer will be at the middle node.
Q6. Travelling Salesman Problem
The Travelling Salesman Problem is a classic optimization problem where a salesman needs to visit a set of cities exactly once and return to the starting city with the shortest possible route.
The goal is to find the shortest possible route that visits each city exactly once and returns to the starting city.
This problem is NP-hard, meaning there is no known efficient algorithm to solve it for large numbers of cities.
One common approach is to use heuristics like the nearest nei...read more
Top Interview Questions from Similar Companies
Reviews
Interviews
Salaries
Users/Month