Primitive and Non-Primitive Data Types in JavaScript
In JavaScript, data types are categorized into primitive and non-primitive types. Primitive data types include string
, number
, boolean
, null
, undefined
, bigint
, and symbol
, which hold single values and are immutable. Non-primitive data types, such as objects, arrays, and functions, store collections of data and are mutable. Understanding these distinctions helps in efficient memory management and coding best practices.
Custom Hooks in React
React allows developers to create custom hooks, which are reusable functions that encapsulate logic for managing state and side effects. By using custom hooks, you can avoid code duplication and make components cleaner. These hooks follow the use
naming convention, like useFetchData
or useAuth
, and enhance component reusability while keeping logic separate from UI concerns.
Minimum Spanning Tree Algorithm
A minimum spanning tree algorithm (MST) is a subset of edges in a weighted graph that connects all vertices with the smallest possible total edge weight. Algorithms like Kruskal’s and Prim’s help in finding the MST efficiently. This is widely used in network design, circuit design, and clustering applications.
Call, Bind, and Apply in JavaScript
These three methods are used to manipulate the this
keyword in JavaScript. Call invokes a function with a specified this
context and arguments passed individually. Apply works similarly but takes arguments as an array. Bind returns a new function with a fixed this
context, useful for preserving object references in event handlers.
What is a Minimum Spanning Tree?
A minimum spanning tree is a connected, acyclic subset of a weighted graph that has the least total weight while covering all vertices. It helps in optimizing network connections, reducing redundancy, and minimizing costs in real-world applications such as transportation and telecommunications.
Dijkstra’s Shortest Path Algorithm
Dijkstra’s algorithm finds the shortest path between nodes in a weighted graph by using a priority queue. It continuously selects the node with the smallest known distance and updates its neighbors. This algorithm is widely applied in GPS navigation, network routing, and game development for pathfinding.
Algorithm in Python
Algorithms in Python are implemented using control structures like loops, conditionals, and recursion. Popular algorithms include sorting (QuickSort, MergeSort), searching (Binary Search), and graph algorithms (BFS, DFS). Python’s built-in libraries, like heapq
for priority queues and networkx
for graphs, simplify algorithm implementation.
Huffman Coding Example
Huffman coding is a lossless compression technique that assigns shorter binary codes to frequent characters and longer codes to infrequent ones. For example, in a text with A(45%), B(20%), C(15%), D(10%), and E(10%), Huffman coding builds a binary tree to generate optimal encodings, reducing overall data size efficiently.
Cannot Read Properties of Undefined
This JavaScript error occurs when trying to access an undefined variable’s properties. It usually happens due to missing data, incorrect object references, or asynchronous operations. Using optional chaining (?.
), default values (||
or ??
), and proper error handling can prevent such issues.
Difference Between Dynamic Programming and Greedy Method
Dynamic programming solves problems by breaking them into overlapping subproblems and storing results for future use, while the greedy method makes the best local choice at each step without considering the global solution. Dynamic programming is used in Fibonacci series and the Knapsack problem, whereas greedy algorithms are applied in Huffman coding and Prim’s algorithm.