wcywxq Algorithm and Programming Notes

website·Indexed 29 days ago

https://wcywxq.github.io

A comprehensive collection of programming notes covering algorithms (divide and conquer, dynamic programming, sorting, backtracking, greedy), data structures (heaps), and practical business scenarios. Includes JavaScript implementations of Promise concurrency, EventBus, and virtual scrolling, as well as LeetCode solutions and React Fiber analysis.

Tokens
73.8K
Snippets
343
Records
384
Agent score
75%

What's inside wcywxq Algorithm and Programming Notes

  1. Understand core Object-Oriented Programming concepts

    Object-Oriented Programming (OOP) is based on several fundamental concepts:

    • Class: A blueprint or template for objects that defines shared data members (attributes/state) and member functions (methods/operations).
    • Object: An instance of a class with its own specific attribute values, capable of executing the methods defined by the class.
    • Abstraction: The process of extracting common characteristics from concrete implementations, typically achieved via Interfaces or Abstract Classes. Abstract classes cannot be instantiated and may contain abstract methods that subclasses must implement.
    • Message Passing: The mechanism where objects interact by calling methods on one another to request services.
    • Composition vs. Aggregation:
      • Composition is a strong association where the internal object cannot exist without the external object.
      • Aggregation is a weaker association where the part can exist independently of the whole.
  2. Advantages of using Svelte for component libraries

    Svelte's runtime architecture allows it to be used to create universal component libraries that are compatible with other frameworks such as Vue, React, and Angular.
  3. Understand the React Fiber Architecture

    React Fiber is a complete rewrite of the React core algorithm. It replaces the 'Stack Reconciler' (which used an uninterruptible recursive process that could freeze the UI during long Diffing operations) with the 'Fiber Reconciler'.

    Fiber transforms the virtual DOM tree into a linked-list structure (using child, sibling, and return pointers). This allows the reconciliation process to be based on a loop rather than recursion, enabling React to pause, abort, or reuse work based on priority, ensuring the main thread remains responsive to user input.

  4. Understand the concept of Function Currying

    Function Currying (also known as Partial Evaluation) is a programming technique that transforms a function with multiple arguments into a series of functions that each take a single argument. Each call returns a new function until all required arguments are received, at which point the original function is executed and the final result is returned. This technique relies on closures and allows for more flexible parameter handling, the creation of specialized function factories, and easier function composition.
  5. Optimize heavy JavaScript computation tasks

    Depending on the environment, use the following strategies to optimize heavy JS calculations:

    • In a DOM environment: Use a Fiber-like architecture (time-slicing) to break large tasks into smaller chunks to avoid blocking the main thread.
    • In a non-DOM environment: Offload computations to a Web Worker to run them in a separate background thread.
  6. Understand Interface Segregation and Dependency Inversion Principles

    Two additional SOLID principles for maintaining low coupling:

    • Interface Segregation Principle (ISP): A class should not be forced to implement interfaces it does not use. Dependencies should be based on the smallest possible interface.
    • Dependency Inversion Principle (DIP): High-level modules should not depend on low-level modules; both should depend on abstractions. This ensures that classes inherit only the minimum necessary interfaces.
  7. Implement Abstraction, Message Passing, and Object Relationships

    Key OOP structural concepts include:

    • Abstraction: Extracting common characteristics from concrete implementations, typically via Interfaces or Abstract Classes. Abstract classes cannot be instantiated and may contain abstract methods that subclasses must implement.
    • Message Passing: The mechanism where objects interact by calling methods on one another to request services.
    • Composition: A strong association where one object contains others as internal parts; if the parent object is destroyed, the internal objects are also destroyed.
    • Aggregation: A weaker 'part-of' relationship where the parent owns the part, but the part can exist independently of the parent.
  8. Apply the three core characteristics of OOP

    The three pillars of OOP are:

    1. Encapsulation: Binding data and methods together and hiding internal implementation details. Access and modification of state are only permitted through public interfaces to ensure data security.
    2. Inheritance: Allowing a derived class to inherit attributes and methods from a base class, enabling code reuse and hierarchical design. Subclasses can add new attributes or override existing methods.
    3. Polymorphism: The ability of a single interface to represent different underlying forms. This includes static polymorphism (compile-time, e.g., function overloading) and dynamic polymorphism (runtime, e.g., virtual functions).
  9. Understand the concept of function debouncing

    Debouncing is a technique used to ensure that a function is not called too frequently. When an event is triggered multiple times in rapid succession, the handler is only executed after a specified wait interval has passed since the last trigger. Essentially, the code only executes once there has been enough 'idle time' without new triggers.
  10. Understand basic OOP concepts: Classes and Objects

    In Object-Oriented Programming, a Class is a blueprint or template that defines data members (attributes/state) and member functions (methods/operations) common to all objects of that type. An Object is a specific instance of a class, possessing its own attribute values and the ability to execute the methods defined by its class.
  11. Reverse Nodes in k-Group (LeetCode 25) Problem Overview

    The goal is to reverse nodes of a singly-linked list in groups of size k. If the number of nodes is not a multiple of k, the remaining nodes at the end should remain in their original order. The solution must actually swap the nodes rather than just changing their values.

    Constraints:

    • Number of nodes n: 1 <= k <= n <= 5000
    • Node values: 0 <= Node.val <= 1000
    • Space complexity goal: O(1) extra memory.
  12. Understand the purpose and architecture of React Fiber

    React Fiber was introduced in React 16 to solve the UI responsiveness issues caused by the StackReconciler used in React 15. In the StackReconciler, the reconciliation process was recursive and uninterruptible; if the Diffing process took too long, it would block the main thread, making the UI unresponsive (e.g., freezing input fields) because the virtual DOM could not be applied to the real DOM.

    To resolve this, React Fiber implements the following:

    1. Interruptible Scheduling: Uses a scheduler (based on the concept of requestIdleCallback) that allows high-priority tasks to interrupt the reconciler.
    2. Linked-List Structure: Converts the traditional tree-based virtual DOM into a Fiber linked-list structure using child, sibling, and return pointers.
    3. Iterative Traversal: Replaces recursive traversal with a loop-based traversal, allowing the reconciliation process to be paused and resumed.
    4. Foundation for Async Rendering: This architecture provides the necessary foundation for asynchronous rendering (introduced in React 17's Lane architecture) and potential off-main-thread worker computations.