C++ Interview Questions
- 0of 0 votes
AnswersWrite a program to get the user name and age
- D PRAVEEN KUMAR October 04, 2016 in India
Condition:
The name should be more than six characters and it shouldn’t contain any other characters than alphabet(like !,@,#,$,^,& these are not to be present)
The age of the employee must between 18-35| Report Duplicate | Flag | PURGE
HTC Global Services Software Developer C++ - 1of 1 vote
AnswersGet an 0. From user and display it’s prime factor
- D PRAVEEN KUMAR October 04, 2016 in India
Example 24 is 2,2,2,3 and 55 is 5,11| Report Duplicate | Flag | PURGE
HTC Global Services Software Developer C++ - 0of 0 votes
AnswersIn a college library fines are issued according to the following condition
- D PRAVEEN KUMAR October 04, 2016 in India
If return books exceeds the due date
Up to 5 days 20rs fine
6-10 days 50rs fine
>10 days 100rs fine
More than 30 days membership will be cancelled
Get the due date from the user and calculate| Report Duplicate | Flag | PURGE
HTC Global Services Software Developer C++ - 0of 0 votes
AnswersWrite a program to get a string and to convert the 1st letter of it to uppercase ?
- D PRAVEEN KUMAR October 04, 2016 in India| Report Duplicate | Flag | PURGE
HTC Global Services Software Developer C++ - 0of 0 votes
AnswerWrite a program to create a sentence at runtime and count number of vowels in it ?
- D PRAVEEN KUMAR October 04, 2016 in India| Report Duplicate | Flag | PURGE
HTC Global Services Software Developer C++ - 0of 0 votes
AnswersWrite a program to calculate the following i want a c++program for this condition 1+4+9+16+….+100
- D PRAVEEN KUMAR October 04, 2016 in India| Report Duplicate | Flag | PURGE
HTC Global Services Software Developer C++ - 0of 0 votes
AnswersWrite a class stringOperation to check whether the characters entered by the user are same or not
- D PRAVEEN KUMAR October 04, 2016 in India| Report Duplicate | Flag | PURGE
HTC Global Services Software Developer C++ - 0of 0 votes
Answerswhy we need interface ( pure virtual function or abstract class) in c++?
- sanjay.pu October 03, 2016 in United States
Instead of having abstract class we can have a base class with virtual function defined in it, and override that virtual function in derived class.
what would be the advantage and disadvantage with the above approach ( except we can create the object of the base class)?| Report Duplicate | Flag | PURGE
Alcatel Lucent Software Engineer / Developer C++ - 0of 2 votes
Answerpair programming example question with code for thoughworks interview
- rahulgoyal030 August 16, 2016 in India| Report Duplicate | Flag | PURGE
ThoughtWorks Software Developer C++ - 0of 0 votes
AnswersFind out the output. Or Correct it if something is wrong.
#include <iostream> #include<typeinfo> using namespace std; class base{ public: int a; base():a(0) {} int getA(){return a;} }; class der:public base { public: int b; der():b(1) {} int getB(){return b;} }; void display(base *obj, int ele) { for(int i = 0; i < ele; i++) { cout << (obj+i)->getA() <<endl; } } int main() { int i = 3; base arrb[i]; display(arrb, 3); der arrd[i]; display(arrd, 3); return 0; }
The output is looking like
0 0 0 0 1 0
To me the output should be
0,0,0,0,0,0 //6 0's
But, how come
1
is coming in?
- rasmiranjanbabu July 12, 2016 in United States| Report Duplicate | Flag | PURGE
Bloomberg LP Software Analyst C++ - 0of 0 votes
AnswersOur merchants receive "weekly" invoices, following these rules:
- deepakmr2008 May 02, 2016 in United States
- Each Saturday (00:00 UTC) marks the beginning of a new billing period
- Each 1st of a month (00:00 UTC) marks the beginning of a new billing
period
- Within a year, billing periods are numbered consecutively, starting
with billing period number 1 on Jan 1
Billing periods can therefore be identified by a year and a period
number.
Task:
-----
Write the following functions:
*) For a given date, return the id of the latest period that ends
before this date, e.g.
auto getLatestBillingPeriodId(Date date) -> PeriodId;
*) For a given period id, return the begin and the end date of the
billing period, e.g.
auto getDateRange(PeriodId periodId) -> std::pair<Date, Date>;| Report Duplicate | Flag | PURGE
C++ - 0of 0 votes
AnswersA flipping rule is given as a follows: Consider a series of positive integer. Take three numbers in the series next to each other. On applying the flipping rule to these numbers, the right most number will go to the left most number position and the other two numbers will move one position to the right at the same time. The rule can be applied to any three numbers present next to each in the series and can be applied as many times as needed.
- Info.Dubey March 03, 2016 in India
Given n as the number of element in the original series, elements of the original series and a target series of a numbers, figures out if the target series can be created by flipping numbers of the original number and output the word “POSSIBLE” followed by the number of times the flipping rule has to be applied. In case, the target series cannot be formed, output the word “IMPOSSIBLE”.
Example :
For a series with 4 elements in it, 1 3 4 2 a new series = 4 3 2 1 can be formed by applying flipping rule as follows, From the table below we can say the output is POSSIBLE 3.
Steps
Series
The three Numbers Flipped
Resultant Series
1
1 3 4 2
1 3 4
4 1 3 2
2
4 1 3 2
1 3 2
4 2 1 3
3
4 2 1 3
2 1 3
4 3 2 1
Example input
Example OutPut
4 1 3 4 2 4 3 2 1
POSSIBLE 3
6 1 2 3 4 5 6 6 5 4 3 2 1
IMPOSSIBLE| Report Duplicate | Flag | PURGE
Infosys Software Developer C++ - 0of 0 votes
AnswersDescription:
- nasbtv January 15, 2016 in India
A company, create classes for each type of employee and calculate working hours and wages/salaries that will be received.
Example General Manager, IT Manager, Accounting, Marketing, Finance, Procurement Managers
Manager and Higher level employees wont have overtime wage. Overtime wage is 1.5 times higher than the usual wage. Working hours are limited as 8 hours. More than this limit will be considered as overtime.
Inputs:
Employee Name, Surname
Title/Role
Salary
Daily Working hour
Outputs:
Date
Employee Name, Surname
Daily Wage.| Report Duplicate | Flag | PURGE
Amazon SDE-2 C++ - 1of 1 vote
AnswersAssume that const_cast is not in place for C++, can you please write the code to do such casting?
- johnsvakel December 14, 2015 in India for MTS| Report Duplicate | Flag | PURGE
Symantec Senior Software Development Engineer C++ - 0of 0 votes
AnswersHow would you implement an LRU cache using just a *single* container ? i.e., map or unordered_map ?
- pavel.em October 07, 2015 in United States
The cache must support operations:
1. value_t find(key_t) - find a certain value in cache
2. insert(key_t, value_t) - insert a new value to the cache (with optionally deleting an LRU entry)| Report Duplicate | Flag | PURGE
Software Engineer C++ - 0of 0 votes
AnswersThere is a garden of strawberry plants represented by a 2D, square array.Each plant represents an element in the matrix ie it has a number of strawberries. If a plant doesnt have strawberries it is denoted by 0. If 0 is encountered you cannot travel through that path.
- topCoder September 27, 2015 in United States
You can start from any cell along the left border of this ground (i.e the matrix) and travel until it finally stops at one cell in the right border, and you can only move to up/down/right. You can only visit each cell once. Calculate the maximum number of berries is obtained.
Backtracking using Dynamic programming is one of the methods i have thought of.
Also there some special conditions:
a.Even in the left border and right border, we can go up and down.
b. When we are at the top cell of one column, we can still go up, which demands us to
pay all current strawberries , then we will be teleported to the bottom cell of this column and vice
versa.
Input: user enters dimensions of ground ie size of matrix and the matrix itself
Output: is the maximum number of strawberries collected without encountering 0; in case we do we display 0.
Till now i have managed to find the largest value in the first column of the matrix but i am facing difficulty in testing the neighbours of that cell.
Also i am not able to store the position of the cell which i started from or even mark it.
Input
4 4
-1 4 5 1
2 -1 2 4
3 3 -1 3
4 2 1 2
output
23
Input
4 4
-1 4 5 1
2 -1 2 4
3 3 -1 -1
4 2 1 2
output
22| Report Duplicate | Flag | PURGE
Walmart Labs Algorithm C++ Coding Dynamic Programming - 0of 0 votes
AnswersConsider the problem of building a wall out of 21 and 31 bricks (horizontalvertical dimensions) such that, for extra strength, the gaps between horizontally-adjacent bricks never line up in consecutive layers, i.e. never form a "running crack".
- professionalid2000 September 17, 2015 in United States
There are eight ways of forming a crack-free 93 wall, written W(9,3) = 8.
Calculate W(32,10).
I need solution for this in C / C++ asap
.
Thanks much in advance for your help.
Vivek.| Report Duplicate | Flag | PURGE
C++ - 6of 6 votes
AnswersPost order traversal for an N-ary tree iterative way.
- hm September 14, 2015 in United States
Given,
struct Node {
int val;
vector<Node*> children;
};
Without modifying original structure.| Report Duplicate | Flag | PURGE
Google Software Engineer Algorithm C++ Trees and Graphs - 0of 0 votes
AnswersPost order traversal for an N-ary tree iterative way.
- hm September 14, 2015 in United States
Given,
struct Node {
int val;
vector<Node*> children;
};
To simplify you can modify the structure.| Report Duplicate | Flag | PURGE
Google Software Engineer Algorithm C++ Trees and Graphs - 0of 0 votes
AnswersImplement a method for the following signature:
void * alignedAllocate(size_t sizeInBytes, size_t alignment) { }
The method should allocate memory for the given size and the pointer should be aligned.
For example ifp = alignedAllocate(1000,64);
p%8 should be 0.
- thewhatwhat September 05, 2015 in United States
Implement a second method that deleted the pointer give p.
Extend the delete method to handle multiple p's.| Report Duplicate | Flag | PURGE
Google Software Engineer C C++ - 0of 0 votes
Answers// Given the root node of a tree in which each node can contain ANY number of chidren (i.e. NOT binary, unbalanced), find the level in the tree with the most nodes
- kaushikjp August 03, 2015 in United States
/*
A Level 0
/ \
B C Level 1
/|\ ||
abc ef
Answer: Level 1| Report Duplicate | Flag | PURGE
Symantec Principal Software Engineer C++ - 0of 0 votes
AnswersGiven a BST write a function that looks for a value.
- rv July 14, 2015 in United States| Report Duplicate | Flag | PURGE
Bloomberg LP Financial Software Developer Algorithm C++ Data Structures - 1of 1 vote
AnswersSort a matrix such that rows in ascending order and columns should be in descending order.
- ritwik_pandey July 05, 2015 in India| Report Duplicate | Flag | PURGE
Amazon SDE-2 C++ - 1of 1 vote
AnswerSkynet
- glory for dreams July 02, 2015 in United States
Skynet has grown to become the dominant force on earth and has almost completely wiped out the
human race. Skynet has been
building robots ever since it's inception and has been updating it's models every year while making
them better. Skynet wants to annihilate humanity completely. It plans to remove one last band of
humans lead by John Connor. Skynet thinks it can destroy these humans using only two of it's robots.
But Skynet doesn't want to send two robots with the same model number lest John Connor finds out a
weakness in that model and easily destroy both of them.
Skynet has at its disposal N robots and to save space Skynet has stored information about pairs of
robots belonging to the same model. If it doesn't have any info stored for a particular robot then it is
implied that the robot is the only one in that model.
Given these constraints, in how many ways can Skynet pick two robots to destroy John Connor and
his rag tag group of humans.
Inputs
N Total
number of robots. Each robot is assigned a number from 0 to N1
(2 <= N <= 100000)
P Number
of pairs for which Skynet has information (2 <= P <= 100000)
This is followed by P pairs. Each pair has two numbers P and P each where 0<=P <=N1
and
0<=P <=N1
and P != P
Output
Number of ways in which Skynet can select 2 robots such that both the robots are different models.
Example Input:
4 2
0 1
2 3
Example Output:
4
Explanation:
Here robots 0 and 1 are of one model, say model A. And 2 and 3 are of another model, say B.
Therefore the total number of
possibilities of picking 2 robots such that no two robots are of the same model are (
0, 2), (0, 3), (1, 2)
and (1, 4) = 4| Report Duplicate | Flag | PURGE
National Instruments SDE1 C++ - 0of 0 votes
AnswersWhat is sister delegation and how is it implemented ?
- Anand Barnwal June 18, 2015 in India| Report Duplicate | Flag | PURGE
Adobe Intern C++ - 0of 0 votes
AnswersWhat design pattern you will use in this scenario :
- S@iR@m June 09, 2015 in India
One class will print odd numbers and other class will print even numbers.| Report Duplicate | Flag | PURGE
IBM Tech Lead C++ - -1of 1 vote
AnswersHow to register new classes in factory pattern ?
- jkl May 30, 2015 in India| Report Duplicate | Flag | PURGE
Tricon Software Developer C++ - 4of 4 votes
AnswersGiven an array of integers and a number. WAP to find the pairs which sum of upto given number.
- Nitin Gupta May 15, 2015 in India for Cloud & Enterprise team
I solved it. Then he asked about writing test cases for this function.
I wrote below test cases
1.) All the elements should be number.
2.) Length of array should not be 0.
3.) Array itself should not be null.
4.) Given number, arrayLength can be represented by 32bits or 64 bits.
5.) number should not be negative.
6.) Input does not has pair, It should return false
7.) Input has pair, It should return true
8.) Input has all negative values and pair exists, then function should return true
9.) Input has all negative values and pair does not exists, function should return false
He told that he is looking for more test cases. Can you guys think of some more complex test cases.| Report Duplicate | Flag | PURGE
Microsoft SDE-2 Algorithm Arrays C++ Data Structures - 0of 0 votes
AnswersInsert a value into a sorted linked list.
- zsalloum May 01, 2015 in United States
Using C/C++ write a small function (around 5 lines in the body) to insert a value in a sorted linked list. Take into consideration that the list might be empty at first, and the function should cover the cases of insertion at the head and tail...
PS what the interviewer is looking for is the ability to write a small C/C++ code that solves the question and not the algorithm per se which is trivial| Report Duplicate | Flag | PURGE
Microsoft Jr. Software Engineer C C# C++ Linked Lists - 0of 0 votes
AnswersGLaDOS is feeling bored, so she decided to come up with a board game. The game is as follows. There is
- glory for dreams April 24, 2015 in India
board of dimension n x n (2 <= n <= 10). Each position in this board is either a 0 or a power of 2, between 2
and 2048. Once the board is set up, there are only two moves allowed - move all left or move all right.
The way move all left works is as follows:
For every row on the board, starting from the rightmost position each element is moved to its left. An
element with a zero value does not move. An element with non-zero value can move to its left if the value of
the element to its left is a 0 or has the same value as the current element.
In case, the element to the left is 0 then the element and 0 swap positions i.e., 4 0 0 4 would become 4 0 4 0
In case, the element to the left has the same value as the current element then the left element combines
with its right element and creates an element with double the value in place of the right element and leaves
a 0 in its current place. For e.g., 2 2 would become 4 0 or 2 2 2 2 would become 4 0 4 0.
The combining operation can cause a cascading operation i.e., if the new element created has the same
value as the element to its left, it can combine again.
For e.g., if a row had 8 4 2 2, move left would combine 2 and 2 to form 4 leading to 8 4 4 0. Now, it is
possible to combine further as the element to the left of 4 has the same value, thus after the second
combine, the row would be 8 8 0 0. And again 8 and 8 would form a 16. Thus the final values in the row
would be 16 0 0 0.
But if the row was 8 4 2 0 2, then moving left would result in 8 4 2 2 0. The cascading operation is allowed
only after a combination operation, There would no cascading operation if the element is swapped with 0.
Similar rules apply for move all right, wherein for every row elements starting from the leftmost position
move to their right.
You can either choose move all left or move all right operation but not both. Now given a state of the board,
you have determine what will be the maximum value on the board after either move all left or move all right.
Example
3
2 2 0
2 2 4
2 0 2
Move all left would result in:
4 0 0
4 0 4
2 2 0
The maximum value on the board after this move is 4.
Move all right would result in:
0 4 0
0 0 8
0 2 2
In the first row, 2 and 2 combines to form 4. In the second row, left most 2 combines with 2 to form 4. As the
element to its right has a value 4, combination operation cascades to form 8.
The maximum value on the board after this move is 8.
Now of the two operations, the higher of the two maximum values is 8. Thus the expected output is 8.
Input
3
2 2 0
2 2 4
2 0 2
Output
8
Input
3
0 0 4
0 2 2
0 4 8
Output
8
Time limit per test case:
1 second(s)| Report Duplicate | Flag | PURGE
National Instruments Software Developer C C++