Phone Interview Flashcards

1
Q

How much time does it take to retrieve an element if stored in HashMap, Binary tree, and a Linked list? How would it change if you have millions of records?

A

In HashMap it takes O(1) time, in the binary tree it takes O(logN) where N is a number of nodes in the tree and in linked list it takes O(n) time where n is a number of element in the list. Millions of records don’t affect the performance if the data structure is working as expected e.g. HashMap has no or relatively less number of collision or binary tree is balanced. If that’s not the case then their performance degrades as a number of records grows.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between Overriding and Overloading?

A

Overriding is resolved at runtime while overloading is compile time. Also, rules of overriding and overloading are different, for example in Java, method signature of the overloaded method must be different than original method, but in the case of overriding it must be exactly same as an overriding method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference between forking a process and spawning a thread?

A

When you fork a process, the new process will run the same code as parent process but in different memory space, but when you spawn a new thread in existing process, it just creates another independent path of execution but share same memory space.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a critical section?

A

A critical section is the part of a code, which is very important and in multi-threading must be exclusively modified by any thread. Semaphore or mutex is used to protect critical section. In Java, you can use synchronized keyword or ReentrantLock to protect a critical section.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the difference between a value type and a reference type?

A

A value type is a more optimized type and always immutable e.g. primitive int, long, double and float in Java while a reference type points to an object, which can be mutable or Immutable. You can also say that value type points to a value while reference type points to an object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is heap and stack in a process?

A

They are two separate areas of memory in the same process. Talking about Java, the stack is used to store primitive values and reference type to object but actual object is always created in heap. One critical difference between heap and stack is that heap memory is shared by all threads but each thread has their own stack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a strongly typed programming language?

A

In a strongly typed language compiler ensure type correctness, for example, you can not store the number in String or vice-versa. Java is a strongly typed language, that’s why you have different data types e.g. int, float, String, char, boolean etc. You can only store compatible values in respective types. On the other hand, weakly typed language don’t enforce type checking at compile time and they tree values based upon context. Python and Perl are two popular example of weakly typed programming language, where you can store a numeric string in number type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the relationship between threads and processes?

A

A process can have multiple threads but a thread always belongs to a single process. Two processes cannot share memory space until they are purposefully doing inter-process communication via shared memory but two threads from the same process always share the same memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does Immutable class mean?

A

A class is said to be Immutable if its state cannot be changed once created, for example, String in Java is immutable. Once you create a String say “Java”, you cannot change its content. Any modification in this string e.g. converting into upper case, concatenating with another String will result in the new object. An immutable object is very useful for concurrent programming because they can be shared between multiple threads without worrying about synchronization. In fact, the whole model of functional programming is built on top of Immutable objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the difference between a class and an object?

A

A class is a blueprint on which objects are created. A class has code and behavior but an object has both the state and behavior. You cannot create an object without creating a class to represent its structure. The class is also used to map an object in memory, in Java, JVM does that for you.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the difference between an interface and an abstract class?

A

An interface is the purest form of abstraction with nothing concrete in place while an abstract class is a combination of some abstraction and concrete things. The difference may vary depending upon language e.g. in Java you can extend multiple interface but you can only extend on the abstract class. For a more comprehensive discussion see the detailed answer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the difference between iteration and recursion?

A

Iteration uses a loop to perform the same step again and again while recursion calls the function itself to do the repetitive task. Many times recursion result in a clear and concise solution to complex problem e.g. tower of Hanoi, reversing a linked list or reversing a String itself. One drawback of recursion is depth since recursion stores intermediate result in the stack you can only go up to a certain depth, after that your program will die with StackOverFlowError, this is why iteration is preferred over recursion in production code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Difference between binary tree and binary search tree?

A

Binary search tree is an ordered binary tree, where the value of all nodes in the left tree are less than or equal to node and values of all nodes in right subtree is greater than or equal to the node (e.g. root). It’s an important data structure and can be used to represent a sorted structure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Examples of recursive algorithms?

A

There are lots of places where recursive algorithm fits e.g. algorithm related to binary and linked list. A couple of examples of a recursive algorithm is reversing String and calculating Fibonacci series. Other examples include reversing linked list, tree traversal, and quick sort algorithm.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Differences between linked list and array?

A

The most significant difference between them is that array stores its element at the contiguous location while linked list stores its data anywhere in memory. This gives linked list enormous flexibility to expand itself because memory is always scattered. It’s always possible that you wouldn’t be able to create an array to store 1M integers but can do by using linked list because space is available but not as contiguous chunk. All other differences are the result of this fact. For example, you can search an element in array with O(1) time if you know the index but searching will take O(n) time in linked list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Explain the difference between is-a and has-a relationships

A

is-a is inheritance where has-a is composition. Inheritance often uses extends or implements. Composition cannot use polymorphism

17
Q

How does Insertion sort work? Complexity?

A

Starts at beginning, looks for smaller integer, iterates backwards when it finds one, continually comparing to make sure the front-end of the array is always sorted. O(n^2) - best to use if list is already mostly sorted

18
Q

How does selection sort work? Complexity?

A

Goes through array looking for smallest integer, places at current index, increments. O(n^2)

19
Q

How does bubble sort work? Complexity?

A

Runs through array sequentially, compares only neighbor values and swaps if necessary. Restarts from beginning once finished, stops if no swaps are made. O(n^2)

20
Q

How does quick sort work? Complexity?

A

Picks random pivot number, swaps to start if less than pivot. Picks new pivot and iterates. Ω(n log(n)), O(n^2)

21
Q

How are Runtime exceptions different from Checked exceptions?

A

Exceptions that are checked at compile time are called checked, unchecked exceptions occur because of bad programming. All unchecked exceptions are direct sub-class of the RuntimeException class.

22
Q

Singleton Pattern?

A

Singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists in the java virtual machine. The singleton class must provide a global access point to get the instance of the class. Singleton pattern is used for logging, drivers objects, caching and thread pool.

23
Q

When to use the factory method pattern?

A
A class cannot anticipate the type of objects it needs to create beforehand.
A class requires its sub-classes to specify the objects it creates.
You want to localize the logic to instantiate a complex object.
24
Q

Which of the following is called address operator?

a) *
b) &
c) _
d) %

A

b) & operator is called address operator and is used to access the address of a variable.

25
Q

Who created C++?

a) Bjarne Stroustrup
b) Dennis Ritchie
c) Ken Thompson
d) Brian Kernighan

A

a) Bjarne Stroustrup is the original creator of C++ during 1979 at AT&T Bell Labs.

26
Q

Wrapping data and its related functionality into a single entity is known as _____________

a) Abstraction
b) Encapsulation
c) Polymorphism
d) Modularity

A

b) Explanation: In OOPs, the property of enclosing data and its related functions into a single entity(in C++ we call them classes) is called encapsulation.

27
Q

How structures and classes in C++ differ?

a) Classes follows OOP concepts whereas structure does not
b) In Structures, members are private by default whereas in Classes they are public by default
c) Structures by default hide every member
d) Classes and Structures are the same

A

a) Explanation: Structures does not follow OOPs concepts as structures does not keep fields and methods together with their interactions whereas classes do.

28
Q

What does polymorphism in OOPs mean?

a) Concept of allowing overiding of functions
b) Concept of hiding data
c) Concept of keeping things in differnt modules/files
d) Concept of wrapping things into a single unit

A

a) Explanation: In OOPs, Polymorphism is the concept of allowing a user to override functions either by changing the types or number of parameters passed.

29
Q

Which of the following shows multiple inheritances?

a) A->B->C
b) A->B; A->C
c) A,B->C
d) B->A

A

c) Explanation: In multiple inheritance, a single class is inherited from two classes. So in A,B->C, Class C is inherited from A and B, whereas in A->B->C, C from B and B from A called simple inheritance, in A->B; A->C, B and C are inherited from A which is called hierarchical inheritance.

30
Q

How access specifiers in Class helps in Abstraction?

a) They does not helps in any way
b) They allows us to show only required things to outer world
c) They help in keeping things together
d) Abstraction concept is not used in classes

A

b) Explanation: Abstraction is the concept of hiding things from the outer world and showing only the required things to the world, which is where access specifiers private, protected and public helps in keeping our knowledge hidden from the world.

31
Q

Which of the following is correct?

a) Base class pointer object cannot point to a derived class object
b) Derived class pointer object cannot point to a base class object
c) A derived class cannot have pointer objects
d) A base class cannot have pointer objects

A

b) Explanation: C++ does not allow a derived class pointer to point a base class pointer whereas Base class can point to a derived class object. Both base class and derived class can have pointer objects.

32
Q

How many types of polymorphism are there in C++?

a) 1
b) 2
c) 3
d) 4

A

b) Explanation: There are two types of polymorphism in C++ namely run-time and compile-time polymorphisms.

33
Q

How run-time polymorphisms are implemented in C++?

a) Using Inheritance
b) Using Virtual functions
c) Using Templates
d) Using Inheritance and Virtual functions

A

d) Explanation: Run-time polymorphism is implemented using Inheritance and virtual in which object decides which function to call.

34
Q

How compile-time polymorphisms are implemented in C++?

a) Using Inheritance
b) Using Virtual functions
c) Using Templates
d) Using Inheritance and Virtual functions

A

c) Explanation: Compile-time polymorphism is implemented using templates in which the types(which can be checked during compile-time) are used decides which function to be called.

35
Q

Which of the following is correct?

a) C++ allows static type checking
b) C++ allows dynamic type checking.
c) C++ allows static member function to be of type const.
d) C++ allows both static and dynamic type checking

A

d) Explanation: C++ allows both static and dynamic type checking i.e. types are checked by the compiler.

36
Q

Which members are inherited but are not accessible in any case?

a) Private
b) Public
c) Protected
d) Both private and protected

A

a) Explanation: Private members of a class are inherited to the child class but are not accessible from the child class.

37
Q

Which of the following is used to make an abstract class?

a) By using virtual keyword in front of a class declaration
b) By using an abstract keyword in front of a class declaration
c) By declaring a virtual function in a class
d) By declaring a pure virtual function in a class

A

d) Explanation: Abstract class should have at least one pure virtual function. Therefore to declare an abstract class one should declare a pure virtual function in a class.

38
Q

Which of the following is the correct difference between cin and scanf()?

a) both are the same
b) cin is a stream object whereas scanf() is a function
c) scanf() is a stream object whereas cin is a function
d) cin is used for printing whereas scanf() is used for reading input

A

b) Explanation: cin is a stream object available in C++ whereas scanf() is a function available in both C and C++. both are used for reading input from users.