## C++ Interview Questions

- 0of 0 votes
Given a singly linked list, write code to find the most frequent element value in the list and its occurrence.

In case of duplicate number of occurrences, return the element value closest to the head.

A solution of time complexity O(N) average is preferred.

Sample Input: 1 2 3 4 2 3 2

Sample Output: Element 2 occurs 3 time(s)

Sample Input: 4 3 5 3 4 5

Sample Output: Element 4 occurs 2 time(s)

- 0of 0 votes
A king gathers all the men in the kingdom who are to be put to death for their crimes, but because of his mercy, he will pardon one. He gathers the men into a circle and gives the sword to one man. The man kills the man to his left, and gives the sword to the man to the dead man's left. The last man alive is pardoned.

With 5 men, the 3rd is the last man alive.

Write a program that accepts a single parameter: a number N that represents the number of criminals to start with. The program should output the number of the last two men alive.

Example #1: myProgram 5

would output:

5, 3

Example #2: myProgram 7

would output:

3, 7

- 0of 0 votes
You are given two numbers L and R, can you count how many number between L and R inclusive their first digit from left equal to one?

- 0of 0 votes
how to create an object on the stack.

and also make sure that only 5 objects are created for the class

- 1of 1 vote
n points on a 2D space. You observe the points from (0,0) with viewing direction and viewing angle.

Given an array (xn,yn), and a viewing angle v (45 degree), find the direction that can observe max number of points.

- 0of 0 votes
You are given a sorted list of distinct integers from 0 to 99, for instance [0, 1, 2, 50, 52, 75]. Your task is to produce a string that describes numbers missing from the list; in this case "3-49,51,53-74,76-99".

Examples:

[] “0-99”

[0] “1-99”

[3, 5] “0-2,4,6-99”

- -1of 1 vote
Assume (n+1) points on a 2D space. You observe the points from (0,0) with viewing direction and viewing angle.

Given an array (xn,yn), and a viewing angle v (45 degree), find the direction that can observe max number of points.

- 1of 1 vote
Program to print string value if each vowel of associated with value 1 and each consonant associated with value 2 print the sum of string value

Ex if input:a

Print O/p:1

I/p:ab

O/p:1+2=3

I/p:abcd

O/l:1+2+2+2=7

I/p:abcde

O/p1+2+2+2+1=8

- 0of 0 votes
Write a program to add the following

¼+2/4+3/4+5/3+6/3+...

- 0of 0 votes
Write a program to get a string and replace with the mentioned string

Example given string “dinesh” replace with “rame” and display it as “ramesh”

- 0of 0 votes
Write a program to get the user name and age

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

- 1of 1 vote
Get an 0. From user and display it’s prime factor

Example 24 is 2,2,2,3 and 55 is 5,11

- 0of 0 votes
In a college library fines are issued according to the following condition

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

- 0of 0 votes
Write a program to get a string and to convert the 1st letter of it to uppercase ?

- 0of 0 votes
Write a program to create a sentence at runtime and count number of vowels in it ?

- 0of 0 votes
Write a program to calculate the following i want a c++program for this condition 1+4+9+16+….+100

- 0of 0 votes
Write a class stringOperation to check whether the characters entered by the user are same or not

- 0of 0 votes
why we need interface ( pure virtual function or abstract class) in c++?

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)?

- 0of 2 votes
pair programming example question with code for thoughworks interview

- 0of 0 votes
Find 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?

- 0of 0 votes
Our merchants receive "weekly" invoices, following these rules:

- 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>;

- 0of 0 votes
Stanford has to select a team of dodgeball players from its class of 2013. There are n students in the class and each student is identified by his/her student ID, which is between 1 and n. The coach has to select K players out of these n students for his team. But there is a twist, if among the K dodgeball players, a player's ID number evenly divides another player's ID number, then there is a high chance of them getting into a fight. The coach will do his best to select the K players so that no pair of players among them will want to fight one another. But if the game turns out to be very popular, this becomes impossible. Complete the function dodgeBall to return the minimum size of K at which it becomes impossible to choose a dodgeball team that has no fighting?

Input Format:

One line of text, containing the size of the class of 2013, n

Constraints:

1 <= n <= 5,000,000,000

n is guaranteed to be an even number

Output Format:

The minimum size of K that guarantees the existence of 2 players who fight with each other in any K-sized subset of the class.

Sample Input:

4

Sample Output:

3

Explanation:

If the team = {1,2,3}: 1&2 or 1&3 can fight with each other

If the team = {1,3,4}: 1&3 or 1&4 can fight with each other

If the team = {2,3,4}: 2&4 can fight with each other

If K=2, then the teams {3,4} or {2,3} will have no fights. So 3 is the smallest value of K for which any K-sized team, must include a fighting pair.

Sample Input:

2

Sample Output:

2

Explanation:

The team = {1,2}: 1&2 can fight with each other

- 0of 0 votes
A 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.

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

- 0of 0 votes
Description:

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.

- 1of 1 vote
Assume that const_cast is not in place for C++, can you please write the code to do such casting?

- 0of 0 votes
How would you implement an LRU cache using just a *single* container ? i.e., map or unordered_map ?

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)

- 0of 0 votes
There 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.

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

- 0of 0 votes
Consider 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".

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.

- 6of 6 votes
Post order traversal for an N-ary tree iterative way.

Given,

struct Node {

int val;

vector<Node*> children;

};

Without modifying original structure.

- 0of 0 votes
Post order traversal for an N-ary tree iterative way.

Given,

struct Node {

int val;

vector<Node*> children;

};

To simplify you can modify the structure.