Q/A for Frontend Interview Flashcards

1
Q
  • What is the difference between == and ===?
A

== 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  • What is the result of adding 1 to the max value of integer?
A

it will result in an overflow error or wraparound behavior where the value will loop back to the minimum value of the data type.

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

** Why 0.1+0.2 != 0.3 ? How to compare floating numbers?

A

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.

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

** What data structures do you know? Describe some.

A
  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. Trees: Trees are a hierarchical data structure that consists of nodes connected by edges. Each node has a parent and zero or more children.
  6. Graphs: Graphs are a collection of vertices connected by edges. They are used to represent complex relationships between objects.
  7. 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.
  8. Heaps: Heaps are a binary tree-based data structure that satisfies the heap property. They are used to implement priority queues and sorting algorithms.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

** Describe steps of Breadth-First Search (or Depth-First Search) for graphs.

A

“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:

  1. Choose a starting vertex and mark it as visited.
  2. Add the starting vertex to a queue.
  3. 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.
  4. Repeat step 3 until the queue is empty.

Depth-First Search:

  1. Choose a starting vertex and mark it as visited.
  2. Visit the starting vertex.
  3. For each unvisited adjacent vertex of the starting vertex, do the following:
    a. Recursively apply steps 1-3 to the adjacent vertex.
  4. 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.”

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

*** Name Big-O complexities in growth order.

A

“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”

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

*** Name Big-O complexities in growth order.

A

“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”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  • What is class, instance, method, abstract class, interface?
A

“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.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  • What is encapsulation?
A

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.

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

** What is polymorphism in basic terms?

A

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.

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

** What is inheritance? What are the cases when inheritance can be a bad practice? (Yo-yo problem) What would be the solution? (Composition)

A

“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.

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

What is SOLID principles?

A

“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:

  1. Single Responsibility Principle (SRP): A class should have only one reason to change.
  2. Open/Closed Principle (OCP): Software entities (classes, modules, functions, etc.) should be open for extension but closed for modification.
  3. Liskov Substitution Principle (LSP): Subtypes must be substitutable for their base types.
  4. Interface Segregation Principle (ISP): Clients should not be forced to depend on interfaces they do not use.
  5. Dependency Inversion Principle (DIP): High-level modules should not depend on low-level modules. Both should depend on abstractions.”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

*** What are coupling and cohesion?

A

“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.”

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

*** What is an inversion of control principle in general?

A

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.

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

*** What is composition, aggregation and why it is preferable over inheritance in OOP?

A

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.

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

*** Common anti-patterns: what is “god class” and why should we avoid it?

A

A “god class” is an anti-pattern in software development where a single class or module becomes responsible for too many tasks or functionalities, making it overly complex and difficult to maintain. This class tends to have a large number of methods and properties, making it difficult to understand and modify.

The term “god class” comes from the idea that this class has god-like powers and controls everything in the system. This can lead to a lack of cohesion and high coupling between different parts of the system, making it difficult to make changes without affecting other parts of the system.

God classes are problematic because they violate the Single Responsibility Principle (SRP) and the Open-Closed Principle (OCP) of object-oriented programming. SRP states that a class should have only one reason to change, while OCP states that a class should be open for extension but closed for modification. A god class violates both of these principles, making it difficult to maintain and extend the system.

17
Q
  • What is a pure function / shared state / side effect?
A

“A pure function is a function that always returns the same output for a given input and does not have any side effects. It does not modify any external state or variables and only depends on its input parameters.

Shared state refers to a situation where multiple parts of a program or system share access to the same data or resources. This can lead to issues such as race conditions and data inconsistencies if not managed properly.

A side effect is any change that a function or operation makes to the state of the system outside of its own scope. This can include modifying variables, writing to files, or making network requests. Side effects can make code harder to reason about and test, and can lead to unexpected behavior.”

18
Q
  • What is a higher-order function?
A

A higher-order function is a function that takes one or more functions as arguments or returns a function as its result. In other words, it is a function that operates on other functions, either by taking them as arguments or by returning them as results. Higher-order functions are a fundamental concept in functional programming and are used to create more abstract and reusable code. Examples of higher-order functions include map, filter, and reduce in JavaScript.

19
Q

** Pros and cons of FP comparing to OOP and procedural languages. Main use cases.

A

“Functional programming (FP) is a programming paradigm that emphasizes the use of pure functions and immutable data structures. It is often compared to object-oriented programming (OOP) and procedural programming, which are two other popular programming paradigms. Here are some pros and cons of FP compared to OOP and procedural languages, as well as some main use cases:

Pros of FP:

  1. Easier to reason about: FP emphasizes the use of pure functions, which means that they have no side effects and always return the same output for a given input. This makes it easier to reason about the behavior of the program and to test it.
  2. Concurrency: FP is well-suited for concurrent programming because pure functions can be executed in parallel without any risk of race conditions or other concurrency issues.
  3. Modularity: FP encourages the use of small, composable functions that can be easily combined to create more complex functionality. This makes it easier to write modular, reusable code.

Cons of FP:

  1. Steep learning curve: FP can be difficult to learn for programmers who are used to imperative or OOP languages.
  2. Performance: FP can be slower than procedural programming because of the overhead of creating and manipulating immutable data structures.
  3. Limited support: FP is not as widely supported as OOP or procedural programming, which means that there may be fewer libraries and tools available.

Main use cases:

  1. Data processing: FP is well-suited for data processing tasks because it emphasizes the use of pure functions and immutable data structures.
  2. Web development: FP can be used for web development, particularly for server-side programming, because it is well-suited for concurrent programming.
  3. Scientific computing: FP is often used in scientific computing because it is well-suited for complex mathematical calculations and data analysis.”
20
Q

** What is a curried function? What is a partial application?

A

“A curried function is a function that takes multiple arguments and returns a series of functions that each take a single argument. This allows for partial application, where some arguments can be provided upfront and the remaining arguments can be provided later.

Partial application is the process of fixing some arguments of a function and creating a new function with fewer arguments. This new function can then be called with the remaining arguments at a later time. It is a technique used in functional programming to create more reusable and composable functions.”

21
Q

*** What is a functor / endofunctor?

A

“A functor is a mathematical concept that describes a relationship between two categories. It is a mapping between the objects and morphisms of one category to the objects and morphisms of another category, preserving the structure and properties of the original category.

An endofunctor is a functor that maps a category to itself. In other words, it is a functor that takes objects and morphisms from a category and returns objects and morphisms in the same category. Endofunctors are important in category theory and functional programming, where they are used to model computations and transformations.”

22
Q

*** Tail reccursion vs non-tail reccursion.

A

Tail recursion is a type of recursion where the recursive call is the last operation performed in the function. In other words, the function does not perform any additional operations after the recursive call. This allows the compiler to optimize the code by replacing the recursive call with a simple jump to the beginning of the function, which saves memory and improves performance.

In general, tail recursion is preferred over non-tail recursion because it is more efficient and consumes less memory. However, not all recursive functions can be written as tail-recursive functions, and in some cases, non-tail recursion may be necessary.”

23
Q
  • What primitive values exist in JS?
A

“There are six primitive values in JavaScript:

  1. Number: represents numeric values, including integers and floating-point numbers.
  2. String: represents textual data, enclosed in single or double quotes.
  3. Boolean: represents a logical value, either true or false.
  4. Null: represents a deliberate non-value or absence of any object value.
  5. Undefined: represents an uninitialized or non-existent value.
  6. Symbol: represents a unique identifier that can be used as an object property key.”
24
Q
  • How does coercion work? Describe how it compares the following: 3 > 2 > 1.
A

“Coercion is the process of converting a value from one data type to another. In programming, coercion can occur implicitly or explicitly. Implicit coercion happens automatically by the programming language, while explicit coercion requires the programmer to specify the conversion.

In the case of the comparison 3 > 2 > 1, coercion occurs implicitly. The comparison is evaluated from left to right, so the first comparison is 3 > 2. This evaluates to true, which is represented as the integer value 1. The second comparison is then 1 > 1, which evaluates to false, represented as the integer value 0.

Therefore, the comparison 3 > 2 > 1 is equivalent to the expression 1 > 1, which is false.”

25
Q

** Do we have arguments in arrow functions? Can arrow function be a constructor?

A

“Arrow functions do not have their own arguments object. However, they can access the arguments object of the enclosing scope.

Arrow functions cannot be used as constructors because they do not have their own this binding. When an arrow function is used with the new keyword, it will throw a TypeError.”

26
Q

** Spread vs Rest operators?

A

The spead operator is represented by three dots (…). It is used to spread the elements of an array or object into another array or object. It is used to make a copy of an array or object, or to combine multiple arrays or objects into one.

The rest operator is also represented by three dots (…). It is used to collect the remaining elements of an array or object into a new array. It is used to pass a variable number of arguments to a function.

27
Q

*** Nullish coalescing vs logical OR operator?

A

In summary, the main difference between the two operators is that the logical OR operator returns the first truthy value, while the nullish coalescing operator returns the first defined value.”

28
Q
  • What determines the value of this?
A

The value of this is determined by how a function is called. It refers to the object that the function is a method of, or the global object if the function is not a method of any object. The value of this can also be explicitly set using call(), apply(), or bind() methods.

29
Q
  • What is a prototype in js?
A

A prototype in JavaScript is an object that is associated with every function and object in JavaScript. It serves as a blueprint for creating new objects and provides a way to share properties and methods between objects. When a property or method is called on an object, JavaScript first looks for it on the object itself. If it is not found, it looks for it on the object’s prototype. If it is still not found, it looks for it on the prototype’s prototype, and so on, until it reaches the end of the prototype chain. This allows for efficient code reuse and inheritance in JavaScript.

30
Q
  • Callbacks vs promises?
A

Callbacks are functions that are passed as arguments to other functions and are executed when the operation is complete. They are commonly used in Node.js and older versions of JavaScript. However, callbacks can lead to callback hell, where multiple nested callbacks make the code difficult to read and maintain.

Promises, on the other hand, are objects that represent the eventual completion or failure of an asynchronous operation. They were introduced in ES6 and provide a cleaner and more organized way to handle asynchronous operations. Promises can be chained together, making it easier to handle multiple asynchronous operations in a sequential manner.

31
Q

*** setImmediate() vs setTimeout(0)?

A

Both setImmediate() and setTimeout(0) are used to execute a function asynchronously, but there are some differences between them.

setImmediate() is a Node.js specific function that executes a function immediately after the current event loop cycle. It is similar to process.nextTick(), but it allows I/O events to be processed before executing the function. This means that setImmediate() is more efficient than setTimeout(0) when dealing with I/O operations.

setTimeout(0) schedules a function to be executed after a minimum delay of 0 milliseconds. However, the actual delay may be longer depending on the current state of the event loop. This means that setTimeout(0) may not be as efficient as setImmediate() when dealing with I/O operations.

32
Q

*** Tasks vs microtasks in js?

A

“Tasks and microtasks are both part of the event loop in JavaScript, but they differ in their priority and timing.

Tasks are the larger units of work that are added to the event queue. These can include things like I/O operations, rendering updates, and user input events. Tasks are processed in the order they are added to the queue, and they can block the main thread if they take too long to complete.

Microtasks, on the other hand, are smaller units of work that are added to a separate queue called the microtask queue. These include things like Promise callbacks, MutationObserver callbacks, and some types of event listeners. Microtasks have a higher priority than tasks, and they are processed before the next task is executed. This means that microtasks can be used to ensure that certain code runs before the UI is updated or before other tasks are executed.

In summary, tasks are larger units of work that are added to the event queue, while microtasks are smaller units of work that are added to a separate queue with higher priority. Understanding the difference between tasks and microtasks is important for writing efficient and responsive JavaScript code.”

33
Q
  • What are the different types of errors in JavaScript?
A

There are several types of errors in JavaScript:

  1. Syntax errors: These occur when the code is not written correctly and does not follow the rules of the language. For example, missing semicolons, parentheses, or curly braces.
  2. Runtime errors: These occur when the code is syntactically correct but there is an error in the logic of the program. For example, trying to access a variable that has not been defined.
  3. Logical errors: These occur when the code runs without any errors, but the output is not what was expected. For example, a function that returns the wrong value.
  4. Type errors: These occur when a value is not of the expected type. For example, trying to add a string and a number.
  5. Reference errors: These occur when a variable or function is referenced but has not been defined. For example, trying to call a function that does not exist.