learner123
BAN USER- 0of 0 votes
AnswersWhat happens when we pass negative indices to an array? Would the compiler allow it?
- learner123 in United States| Report Duplicate | Flag | PURGE
C++ - 0of 0 votes
Answerswhat container will you choose if you should store numbers and check whether certain number is already stored?
- learner123 in United States| Report Duplicate | Flag | PURGE
C++ - 0of 0 votes
AnswersDesign a logger class for your application? The log messages can be printed in a log file.
- learner123 in United States
Suppose you may want to provide the user the option whether he wants to print the messages in a log file or into database at runtime. How would you modify your earlier design?| Report Duplicate | Flag | PURGE
Object Oriented Design - -1of 1 vote
AnswersLets say you have a number n and there is an array that has size = n+1.There is one element repeated in the array. Can you find that number?
- learner123 in United States
Input spec: n = 5
int arr[6] = {3, 1, 4, 4, 5, 2};
Output: 4| Report Duplicate | Flag | PURGE
Problem Solving - -1of 1 vote
AnswersLets say you have a number n and there is an array whose size is n-1. Array contains numbers in between 1 to n and all unique numbers.
- learner123 in United States
But one number is missing from the array as its size is n-1. Can you find that missing number in the array?
Input Spec: n = 5
int arr[4] = {3, 1, 4, 5};
Output: 2| Report Duplicate | Flag | PURGE
Problem Solving - 0of 0 votes
AnswersLets say you have been given a number n. You need to find out all prime numbers starting from 1..to..n ?
- learner123 in United States| Report Duplicate | Flag | PURGE
Problem Solving - 0of 0 votes
AnswersAsked to explain the abstract factory design pattern along with its drawbacks (if any)?
- learner123 in United States| Report Duplicate | Flag | PURGE
Object Oriented Design - 0of 0 votes
AnswersDo STL containers always create copy of objects when containers are populated (e.g. if you have a vector<A> or a map<int, A>, when we insert elements into vector/map, whether copies of object of class A would be stored inside the vector/map?
- learner123 in United States| Report Duplicate | Flag | PURGE
C++ - 0of 0 votes
AnswersHow can we achieve something similar to polymorphism in C language? Polymorphism is an OOPs feature.
- learner123 in United States| Report Duplicate | Flag | PURGE
C++ - 0of 0 votes
AnswersCan we write a collection class in C++ that can store elements of different data types?
- learner123 in United States| Report Duplicate | Flag | PURGE
C++
Thanks hprem991.
- learner123 May 13, 2014Hi friends, I have tried to code this problem in C++ and tried to show usage of two design patterns ( singleton, observer ). Overall the code is written assuming bare minimum requirements and there is always scope of improvements. The ParkingLot class is a singleton and observer pattern is used to update the balance reports. Please provide your valuable comments.
#include "stdafx.h"
#include <iostream>
#include <list>
#include <vector>
using namespace std;
class Vehicle
{
protected:
double m_vehicleSize;
double m_timeIN;
public:
double GetVehicleSize()
{
return m_vehicleSize;
}
void SetVehicleSize( double size )
{
m_vehicleSize = size;
}
double GetTimeIN()
{
return m_timeIN;
}
void SetTimeIN( double time_in )
{
m_timeIN = time_in;
}
virtual void GetVehicleNumber()
{
}
};
class Car : public Vehicle
{
};
class Truck : public Vehicle
{
};
class BalanceReport
{
double m_balanceAmount;
public:
BalanceReport()
{
m_balanceAmount = 0;
}
void update( double fee )
{
SetBalanceAmount( fee );
}
void SetBalanceAmount( double fee )
{
m_balanceAmount += fee;
}
double GetBalanceAmount()
{
return m_balanceAmount;
}
};
class ParkingLot
{
list<Vehicle*> m_vehiclesList;
vector<BalanceReport*> observers;
double m_parkingLotSize;
double m_remaingParkingSpace;
int m_totalVehiclesEntered;
int m_totalVehicleExited;
static ParkingLot* instance;
ParkingLot()
{
m_parkingLotSize = 100 ;
m_parkingLotSize = 100;
m_totalVehiclesEntered = 0;
m_totalVehicleExited = 0;
}
ParkingLot( double parkingLotSize, double balanceAmount )
{
m_parkingLotSize = parkingLotSize;
m_remaingParkingSpace = parkingLotSize;
m_totalVehiclesEntered = 0;
m_totalVehicleExited = 0;
}
public:
static ParkingLot* GetInstance()
{
if( instance == NULL )
{
instance = new ParkingLot( 500, 0 );
}
return instance;
}
//observer pattern
void AddObserver( BalanceReport* o )
{
observers.push_back( o );
}
void RemoveObserver( BalanceReport* o )
{
observers.pop_back();
}
void EnterVehicleIntoParking( Vehicle* vehicle, double time_in )
{
vehicle->SetTimeIN( time_in );
m_vehiclesList.push_back( vehicle );
if( dynamic_cast< Car* >( vehicle ) )
{
m_remaingParkingSpace -= 1;
}
else
{
m_remaingParkingSpace -= 2;
}
m_totalVehiclesEntered += 1;
}
void NotifyObserver( double parkingFees )
{
for( vector<BalanceReport*>::iterator iter = observers.begin();
iter != observers.end(); ++iter )
{
(*iter)->update( parkingFees );
}
}
void ExitVehicleFromParking( Vehicle* vehicle, double time_out )
{
double parkingFees = 0.0;
double totalTime = time_out - vehicle->GetTimeIN();
if( dynamic_cast< Car* >( vehicle ) )
{
parkingFees = 2 * totalTime;
}
else
{
parkingFees = 3 * totalTime;
}
NotifyObserver( parkingFees );
if( dynamic_cast< Car* >( vehicle ) )
{
m_remaingParkingSpace += 1;
}
else
{
m_remaingParkingSpace += 2;
}
m_totalVehicleExited += 1;
m_vehiclesList.remove( vehicle );
}
void PrintBalanceReport()
{
cout << "Balance Amount: " << observers[0]->GetBalanceAmount() << endl // Assumption: As of now only one observer
<< "Total vehicles entered: " << m_totalVehiclesEntered << endl
<< "Total vehicles exited: " << m_totalVehicleExited << endl
<< "Remaining parking space: "<< m_remaingParkingSpace << endl;
}
};
ParkingLot* ParkingLot::instance = NULL;
int _tmain(int argc, _TCHAR* argv[])
{
ParkingLot* parking = ParkingLot::GetInstance(); //singleton parking lot class
BalanceReport* report = new BalanceReport;
parking->AddObserver( report );
Vehicle* car1 = new Car;
Vehicle* truck1 = new Truck;
parking->EnterVehicleIntoParking( car1, 2 );
parking->EnterVehicleIntoParking( truck1, 3 );
parking->ExitVehicleFromParking( car1, 10 );
parking->ExitVehicleFromParking( truck1, 12 );
parking->PrintBalanceReport();
delete report;
return 0;
}
My understanding is as following apart from the above answers. Please correct me if you think something is not feasible.
1. We need to think whether we want to create an abstract class (making some of its method as pure virtual). If there is a possibility of extending the behaviors of this class we can make some of its member function as virtual.
2. On the other hand, if the class has to used with different data types and there is not much change in the behavior (i.e. member functions) for data types, we can create a template class. For example while writing any container class we may think of making it a template class, so that different data types can be stored inside the container class.
This would depend upon the business use case for which we are designing/writing the class.
-3 x -2 = 6
6 x -3 = -18
-18 x -4 = 72
72 x -5 = -360
-360 x -6 = 2160
so next number in the series is 2160.
( 3 x 2 ) + 2 = 8
( 8 x 2 ) + 2 = 18
( 18 x 2 ) + 2 = 38
( 38 x 2 ) + 2 = 78
so missing number is 38.
Nopes...just practicing for interviews :)
- learner123 April 23, 2014Thanks. Following above suggestion..tried some code. For simplicity using array, but it can always be extended to user defined collection classes.
#include <iostream>
using namespace std;
class BaseDataType
{
public:
virtual void print() = 0;
};
class IntDataType: public BaseDataType
{
int m_x;
public:
void print()
{
cout << "IntDataType" << endl;
}
};
class FloatDataType: public BaseDataType
{
float m_x;
public:
void print()
{
cout << "FloatDataType" << endl;
}
};
class DoubleDataType: public BaseDataType
{
double m_x;
public:
void print()
{
cout << "DoubleDataType" << endl;
}
};
int main()
{
BaseDataType* a[3];
a[0] = new IntDataType;
a[1] = new FloatDataType;
a[2] = new DoubleDataType;
for(int i = 0; i < 3; ++i)
{
a[i]->print();
}
return 0;
}
A simple example to explain:
- learner123 May 13, 2014class Widget
{
public:
virtual void Display() = 0;
};
class WindowsMenuItem : public Widget
{
public:
void Display()
{
cout << "Displaying Menu Item on Windows" << endl;
}
};
class WindowsEditBox : public Widget
{
public:
void Display()
{
cout << "Displaying Edit Box on Windows" << endl;
}
};
class MotifMenuItem : public Widget
{
public:
void Display()
{
cout << "Displaying Menu Item on Motif" << endl;
}
};
class MotifEditBox : public Widget
{
public:
void Display()
{
cout << "Displaying Edit Box on Motif" << endl;
}
};
class AbstractFactoryClass
{
public:
virtual Widget* CreateMenuItem() = 0;
virtual Widget* CreateEditBox() = 0;
};
class WindowsFactory : public AbstractFactoryClass
{
public:
Widget* CreateMenuItem()
{
return new WindowsMenuItem();
}
Widget* CreateEditBox()
{
return new WindowsEditBox();
}
};
class MotifFactory : public AbstractFactoryClass
{
public:
Widget* CreateMenuItem()
{
return new MotifMenuItem();
}
Widget* CreateEditBox()
{
return new MotifEditBox();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
AbstractFactoryClass* factory;
Widget *editBox, *menuItem;
if( Windows )
{
factory = new WindowsFactory();
editBox = factory->CreateEditBox();
menuItem = factory->CreateMenuItem();
}
else
{
factory = new MotifFactory();
editBox = factory->CreateEditBox();
menuItem = factory->CreateMenuItem();
}
editBox->Display();
menuItem->Display();
return 0;
}