Q/A for Frontend Interview Flashcards
- What is the difference between == and ===?
== is a loose equality operator that compares two values for equality after converting their types.
=== is a strict equality operator that compares two values for equality without converting their types.
- What is the result of adding 1 to the max value of integer?
it will result in an overflow error or wraparound behavior where the value will loop back to the minimum value of the data type.
** Why 0.1+0.2 != 0.3 ? How to compare floating numbers?
0.1 and 0.2 are not exact binary fractions, so when they are added together, the result is not exactly 0.3. This is because computers use a binary system to represent floating-point numbers, which can only approximate decimal fractions. Therefore, there can be rounding errors when performing arithmetic operations on floating-point numbers.
To compare floating-point numbers, it is recommended to use a tolerance or epsilon value. This means that instead of checking if two numbers are exactly equal, we check if the difference between them is within a certain range. For example, we can define a tolerance of 0.0001 and check if the absolute difference between 0.1+0.2 and 0.3 is less than or equal to this tolerance. If it is, we can consider the two numbers to be equal.
** What data structures do you know? Describe some.
- Arrays: Arrays are a collection of similar data types that are stored in contiguous memory locations. They are used to store a fixed number of elements.
- Linked Lists: Linked lists are a collection of nodes that are connected to each other through pointers. Each node contains data and a pointer to the next node in the list.
- Stacks: Stacks are a collection of elements that follow the Last-In-First-Out (LIFO) principle. They are used to store and retrieve data in a specific order.
- Queues: Queues are a collection of elements that follow the First-In-First-Out (FIFO) principle. They are used to store and retrieve data in a specific order.
- Trees: Trees are a hierarchical data structure that consists of nodes connected by edges. Each node has a parent and zero or more children.
- Graphs: Graphs are a collection of vertices connected by edges. They are used to represent complex relationships between objects.
- Hash Tables: Hash tables are a data structure that uses a hash function to map keys to values. They are used to store and retrieve data quickly.
- Heaps: Heaps are a binary tree-based data structure that satisfies the heap property. They are used to implement priority queues and sorting algorithms.
** Describe steps of Breadth-First Search (or Depth-First Search) for graphs.
“Breadth-First Search (BFS) and Depth-First Search (DFS) are two popular algorithms used for traversing graphs. Here are the steps for each algorithm:
Breadth-First Search:
- Choose a starting vertex and mark it as visited.
- Add the starting vertex to a queue.
- While the queue is not empty, do the following:
a. Dequeue a vertex from the queue.
b. Visit the dequeued vertex and mark it as visited.
c. Enqueue all adjacent vertices of the dequeued vertex that have not been visited. - Repeat step 3 until the queue is empty.
Depth-First Search:
- Choose a starting vertex and mark it as visited.
- Visit the starting vertex.
- For each unvisited adjacent vertex of the starting vertex, do the following:
a. Recursively apply steps 1-3 to the adjacent vertex. - Repeat step 3 until all vertices have been visited.
Both algorithms can be used to find paths between vertices, determine if a graph is connected, and perform other graph-related tasks. The choice of algorithm depends on the specific problem and the characteristics of the graph being traversed.”
*** Name Big-O complexities in growth order.
“1. O(1) - constant time complexity
2. O(log n) - logarithmic time complexity
3. O(n) - linear time complexity
4. O(n log n) - linearithmic time complexity
5. O(n^2) - quadratic time complexity
6. O(n^3) - cubic time complexity
7. O(2^n) - exponential time complexity
8. O(n!) - factorial time complexity”
*** Name Big-O complexities in growth order.
“1. O(1) - constant time complexity
2. O(log n) - logarithmic time complexity
3. O(n) - linear time complexity
4. O(n log n) - linearithmic time complexity
5. O(n^2) - quadratic time complexity
6. O(n^3) - cubic time complexity
7. O(2^n) - exponential time complexity
8. O(n!) - factorial time complexity”
- What is class, instance, method, abstract class, interface?
“Class: A class is a blueprint or a template for creating objects that define a set of attributes and methods that the objects will have.
Instance: An instance is an individual object created from a class that has its own set of attributes and methods.
Method: A method is a function that is defined within a class and can be called on an instance of that class to perform a specific action.
Abstract class: An abstract class is a class that cannot be instantiated and is used as a base class for other classes. It contains one or more abstract methods that must be implemented by its subclasses.
Interface: An interface is a collection of abstract methods that define a set of behaviors that a class must implement. It is used to define a contract between different parts of a program, ensuring that they can work together seamlessly.”
- What is encapsulation?
Encapsulation is a fundamental concept in object-oriented programming that refers to the practice of hiding the internal details of an object from the outside world and exposing only the necessary information and functionality through a well-defined interface. This means that the internal state of an object can only be accessed and modified through its public methods, while its private data and implementation details are kept hidden and protected from external interference. Encapsulation helps to improve the security, maintainability, and flexibility of software systems by reducing the complexity and dependencies between different components and promoting modular design and code reuse.
** What is polymorphism in basic terms?
Polymorphism is the ability of an object or method to take on multiple forms. In programming, it refers to the ability of an object to be treated as if it is of different types or to have different behaviors depending on the context in which it is used. This allows for more flexible and reusable code.
** What is inheritance? What are the cases when inheritance can be a bad practice? (Yo-yo problem) What would be the solution? (Composition)
“Inheritance is a mechanism in object-oriented programming where a new class is created by inheriting the properties and methods of an existing class. The existing class is called the parent or base class, and the new class is called the child or derived class. Inheritance allows the child class to reuse the code of the parent class, which can save time and effort in programming.
However, there are cases when inheritance can be a bad practice. One such case is the yo-yo problem, which occurs when there are too many levels of inheritance between classes. This can make the code difficult to understand and maintain, as changes in one class can have unintended effects on other classes in the inheritance hierarchy.
The solution to the yo-yo problem is composition, which involves creating objects that contain other objects as members.
What is SOLID principles?
“SOLID principles are a set of five design principles that help developers create software that is easy to maintain, extend, and modify. The principles are:
- Single Responsibility Principle (SRP): A class should have only one reason to change.
- Open/Closed Principle (OCP): Software entities (classes, modules, functions, etc.) should be open for extension but closed for modification.
- Liskov Substitution Principle (LSP): Subtypes must be substitutable for their base types.
- Interface Segregation Principle (ISP): Clients should not be forced to depend on interfaces they do not use.
- Dependency Inversion Principle (DIP): High-level modules should not depend on low-level modules. Both should depend on abstractions.”
*** What are coupling and cohesion?
“Coupling and cohesion are two important concepts in software engineering that describe the relationships between different components or modules of a software system.
Coupling refers to the degree of interdependence between different modules or components of a software system. It measures how closely two or more modules are connected or how much they rely on each other. High coupling means that changes in one module can have a significant impact on other modules, making the system more difficult to maintain and modify. Low coupling, on the other hand, means that modules are more independent and changes can be made more easily without affecting other parts of the system.
Cohesion, on the other hand, refers to the degree to which the elements within a single module or component are related to each other. It measures how well a module or component is focused on a single task or responsibility. High cohesion means that the elements within a module are closely related and work together to achieve a single goal, making the module more reusable and easier to maintain. Low cohesion means that the elements within a module are not closely related and may perform multiple tasks, making the module more difficult to understand and modify.
In general, software systems with high cohesion and low coupling are considered to be well-designed and easier to maintain and modify over time.”
*** What is an inversion of control principle in general?
Inversion of Control (IoC) is a design principle in software engineering that refers to the process of inverting the flow of control in a software system. In traditional programming, the application code controls the flow of execution, but with IoC, the control is inverted, and the framework or container controls the flow of execution. This means that the framework or container manages the lifecycle of objects and controls the interactions between them, rather than the application code. The IoC principle is often used in conjunction with the Dependency Injection (DI) pattern, which allows objects to be injected into a class rather than being created by the class itself. This makes the code more modular, testable, and maintainable.
*** What is composition, aggregation and why it is preferable over inheritance in OOP?
Composition is a relationship where one object is composed of one or more other objects. The composed objects cannot exist without the main object. For example, a car is composed of an engine, wheels, and other parts. If the car is destroyed, the engine and wheels cannot exist on their own.
Aggregation is a relationship where one object is composed of one or more other objects, but the composed objects can exist independently of the main object. For example, a university is composed of departments, and the departments can exist independently of the university.
Inheritance is another way of creating relationships between objects in OOP. It allows a subclass to inherit properties and methods from a superclass. However, inheritance can lead to a complex and inflexible class hierarchy, making it difficult to maintain and modify the code.
Composition and aggregation are preferable over inheritance because they allow for more flexible and modular code. They allow objects to be composed of other objects, which can be easily modified or replaced without affecting the main object. This makes the code more maintainable and easier to understand. Additionally, composition and aggregation can be used to create more complex relationships between objects, such as a hierarchy of objects composed of other objects.