PayPal Interview Question
Software Engineer / Developersno it does not....because the middle class inherits privately all members of the base class...but the last class which inherits the middle class can also access the private methods and data of middle class...thus the last class again ends up with two copeis of 1st base class....hence problem not solved...only way to solve it is by using virtual base class
If we derive one of the middle base class as private then the last base class cant inherit any of its methods. Then the last class will lose some methods. If we derive middle base class all members as public then there is duplicate when last class inherits them. So when we use keyword virtually, then the languages itself handles and gives only one copy. So the problem is solved.
#include<iostream.h>
class a
{
int var;
public:
a()
{
var=10;
}
void fn()
{
cout<<var<<endl;
}
};
class b: private a
{
};
class c:public a
{
};
class d:private b, public c
{
};
int main()
{
d d1;
d1.fn();
}
In this still ambiguous error is thrown. Which is solved only when we declare inheritance as virtual.
coz var is still inherited by class b (only class b object cant access them) and class c.
Hence ambiguity arises
ArrayList is the fastest solution. You can use HashSet too, however AL is a bit faster.
public static void main(String[] args) {
int[] arr = { 1, 3, 67, 3, 6, 88, 9, 56, 4, 5, 7, 23, 4, 99 };
removeDuplicatesUsingAL(arr);
// removeDuplicatesUsingHashSet(arr);
}
public static void removeDuplicatesUsingAL(int[] arr) {
long startTime = System.nanoTime();
System.out.println("Start time: " + startTime);
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < arr.length; i++) {
if (!list.contains(arr[i])) {
list.add(arr[i]);
}
}
for (Integer i : list) {
System.out.println("values are: " + i);
}
long endTime = System.nanoTime();
System.out.println("End time: " + endTime);
System.out.println("total Time is: " + (endTime - startTime));
}
public static void removeDuplicatesUsingHashSet(int[] arr) {
long startTime = System.nanoTime();
System.out.println("Start time: " + startTime);
Set hashSet = new HashSet();
for (int i = 0; i < arr.length; i++) {
if (!hashSet.contains(arr[i])) {
hashSet.add(arr[i]);
}
}
Iterator iter = hashSet.iterator();
while (iter.hasNext()) {
System.out.println("values are: " + iter.next());
}
long endTime = System.nanoTime();
System.out.println("End time: " + endTime);
System.out.println("total Time is: " + (endTime - startTime));
}
Below is a sample run:-
The time taken for AL: 1876787
The time taken for HS: 1756755
we cant use the class which declared as private constructor through instance. So it will be used for adding only class specific static members. If it is used in inheritance , then implicit super constructor loading itself will fail. or if we say public middle class with private members, still we are not achieving multiple inheritance. private members considered only for the middle parent class. So no relationship and sharing.
inheritance always inherits all the bits and services of base class(sizeof confirms this!).. the only difference is access levels.private inheritance doesnot mean that privete members are not inherited.. they are.. but there access isnt given to the derived objects..so the ambiguity problem remains intact weather one of the middle class is privately or publicly derived.So this does not solve the diamond inheritance problem.
- ashish August 03, 2011