DEV Community

Cover image for Day 5/366
vishal.codes
vishal.codes

Posted on • Updated on

Day 5/366

🚀 Today's Learning:

🌟 DSA

  • Intersection of Two Linked Lists - Leetcode 160
  • Insertion in a circular Linked List

🌟 Dev

  • Difference b/w async and defer, which is better.
  • Debouncing
  • Interface and can we add methods in the Interface.
  • Polyfill

🔍 Some Key Highlights:

DSA

Intersection of Two Linked Lists - Leetcode 160

Approach 1 (brute force) →

Traverse both list and find lengths → Find the difference in lengths → Assign q to longer list’s head and p to shorter list’s head → move q to equal starting point as p (no. of steps equal to the difference in lengths) → now move p and q together step by step → the point where they meet is the intersection node.

Approach 2 (optimized) →

→move p and q equally step by step → if p reaches null shift it to headB → if q reaches null shift it to headA → this way, in the next pass they will bypass the difference between them and move parallelly to each other → now just return the node where they meet.

Image description

Insertion in a circular linked list

Cases:

  1. Insert before head → create a new node t. t→next = head. Traverse a pointer starting from head to the end node (while p→next ! = NULL). So now p will be at last node. So, p→ next = t. Head = t.
  2. Insert at a given position (say after nth node) → create a new node t. Traverse a pointer p by n-1 times. Now p is at the node after which new node t will be inserted. t→next = p→next. p→next=t.
  3. List is sorted → create a new node t. Take two pointers *prev and *curr. *prev = head and *curr = head→ next. Now slide them forward a step but at every step check if the value to be inserted has to be inserted at this step only. If it does then break the loop otherwise keep moving till curr ! = head. (Note: carefully note that there will be two cases to determine the correct position of insertion - one is just the insert value should be greater than prev→value and small than curr→value. Other is when prev→val > curr→val. This is the point where the LL circularizes. So at this point either the insert val should be greater than prev→val or it should be less than curr→val.) After determining the correct position of insertion do prev→next = t. t→next = curr and return head.

DEV

  1. Async vs. Defer:
    • Both async and defer attributes in HTML script tags allow for asynchronous loading of scripts.
    • Async downloads and executes the script asynchronously, without blocking the HTML parsing. Order of execution may vary.
    • Defer also loads scripts asynchronously but ensures they execute only after the HTML parsing is complete, maintaining execution order.
  2. Debouncing:
    • Debouncing prevents a function from being called too frequently, especially during rapid events like scrolling or typing.
    • It's like waiting for someone to finish an action before reacting.
    • Commonly used in scenarios like search bars to reduce unnecessary API calls.
  3. Interface:
    • An interface in programming defines a contract specifying methods and properties a class must have.
    • It's like a blueprint for objects, ensuring consistency and structure.
    • In some languages (like Typescript), interfaces can include method declarations without implementations.
  4. Polyfills:
    • Polyfills are code snippets that provide modern functionality to older browsers.
    • We created polyfills for array.map() and Promise.all(), ensuring compatibility across different environments.

Top comments (0)