qwertyvipul.io Documentation
website·Indexed 29 days ago
https://qwertyvipul.ioA collection of technical notes and guides covering LeetCode solutions in Python and JavaScript, system design principles including the scalable monolith, and programming language fundamentals for Go, Rust, and JavaScript. Includes specific sections on OAuth, token-based authentication, and database types.
What's inside qwertyvipul.io
- Go is a statically typed language featuring garbage collection, type safety, and built-in support for concurrency. It is designed to be simple, efficient, and scalable, making it particularly well-suited for building large, distributed systems.
Implement a Scalable Monolith layered architecture
The scalable monolith architecture organizes code into layers with increasing authority. The primary goal is to ensure responsibilities remain explicit, execution flow is visible, and state changes are centralized.
Layer Definitions:
- API Handlers: The system edge. Handles input parsing, request-level validation, authentication, and rate limits. Calls Workflows or Integrations.
- Integrations: Coordinates multiple workflows. Defines execution order and data handoff. Contains no domain logic and manages no database sessions.
- Workflows: The center of the system. Defines the execution of a single cohesive operation, controls sequence, manages the database session lifecycle (creation, commit, rollback), and handles all state changes.
- Helpers: Pure domain logic. Performs calculations, complex validations, and rules operating on data. Executes entirely in memory (no I/O).
- Utils: Low-level operations. Interacts with the outside world, performs I/O, or calls external infrastructure. They are terminal and do not call other utils.
- Models: Defines data shapes only; behavior resides in other layers.
Understand SQL Relational Database characteristics
SQL databases are relational systems that organize data into tables consisting of rows and columns. They utilize Structured Query Language (SQL) for data definition, manipulation, and querying. Common examples include MySQL, PostgreSQL, Oracle DB, MS SQL Server, and SQLite.Understand the fundamentals of System Design
System design in software engineering is the process of translating specified requirements into a structured system for effective implementation. It involves defining five core elements:
- Architecture: The high-level blueprint outlining component arrangements, relationships, and guiding principles to ensure scalability, reliability, and performance.
- Components: The individual building blocks of the system.
- Modules: Self-contained software units.
- Interfaces: The methods, protocols, and data formats that define how components communicate.
- Data: The fundamental building blocks of information used by the system.
Understand the JavaScript Event Loop mechanism
The JavaScript runtime manages asynchronous execution using three primary components: the Call Stack, the Message Queue, and the Event Loop. The Event Loop continuously monitors the Message Queue; when messages (callbacks/tasks) arrive, they are processed in First-In-First-Out (FIFO) order and pushed onto the Call Stack for execution.Implement the OAuth sign-in and token exchange flow
The standard OAuth authentication flow involves delegating identity verification to a trusted provider (e.g., Google, Apple, Microsoft) and managing sessions via tokens:
- Provider Authentication: The frontend opens the provider's auth page. Upon success, the provider returns a token to the frontend, which is then forwarded to the backend.
- Backend Verification: The backend validates the provider's token, extracts user identity (email/ID), and creates/retrieves the user record in the database.
- Session Establishment: The backend issues two tokens to the frontend: a short-lived Access Token for API calls and a long-lived Refresh Token for session renewal.
- Authenticated Requests: The frontend stores these tokens and attaches the access token to protected API calls.
- Token Renewal: When the access token expires, the frontend sends the refresh token to a dedicated backend endpoint to obtain a new access token without requiring the user to re-authenticate.
Implement Refresh Token Rotation for enhanced security
To prevent replay attacks and limit the impact of a compromised token, implement refresh token rotation. When a refresh token is used to obtain a new access token, the backend should:
- Issue a new access token.
- Issue a new refresh token.
- Invalidate the old refresh token immediately.
Simulate and aggregate Rubrik API data using Promise.all
This example demonstrates how to handle multiple asynchronous API calls to different data centers and applications, aggregating the results (protected and unprotected counts) into a final summary object. It usesPromise.allto wait for all simulated API requests to resolve before calculating the totals per data center.const appNames = ["vmware", "kafka", "zookeeper"]; const dataCenters = [ { name: "london", url: "ldn.api.com" }, { name: "hongkong", url: "hk,api.com" }, { name: "bangalore", url: "blr.api.com" }, ]; // Mock database generation const db = []; for (const { name, url } of dataCenters) { const entry = { name, url }; for (const appName of appNames) { entry[appName] = { protectedCount: Math.floor(Math.random() * 200) + 100, unProtectedCount: Math.floor(Math.random() * 200) + 100, }; } db.push(entry); } // Simulate API call const apiCall = (url, appName) => { return new Promise((resolve) => { const data = db.find((entry) => entry.url === url)[appName]; setTimeout(() => resolve(data), Math.random() * 2000 + 1000); }); }; // Execute concurrent requests const promises = []; for (const { name, url } of dataCenters) { for (const appName of appNames) { promises.push(apiCall(url, appName).then((data) => ({ name, data }))); } } const res = {}; Promise.all(promises).then((values) => { values.forEach(({ name: dataCenterName, data }) => { if (!res[dataCenterName]) { res[dataCenterName] = { totalProtectedCount: 0, totalUnprotectedCount: 0 }; } res[dataCenterName].totalProtectedCount += data.protectedCount; res[dataCenterName].totalUnprotectedCount += data.unProtectedCount; }); console.log("Aggregated Results:", res); });Implement memoization using functools.lru_cache
Use the@functools.lru_cachedecorator to memoize expensive functions in Python. This stores the results of function calls, returning the cached value when the same inputs occur again. You can specify themaxsizeparameter to limit the number of entries in the cache.import functools @functools.lru_cache(maxsize=4) def expensive_function(n): print(f"Calculating {n}...") return n * nManipulate arrays and implement queues in JavaScript
JavaScript arrays support dynamic sizing. Usepush()andshift()to implement a basic Queue (FIFO). Useunshift()to add to the front andshift()to remove from the front. Useslice()for non-destructive copying andsplice()for modifying the original array.// Initialization const arr = Array(n).fill(0); const fromArray = Array.from(iterable); // Queue operations const q = []; q.push(num); // Enqueue const front = q.shift(); // Dequeue // Array manipulation arr.slice(startIdx, endIdx); // Returns new copy arr.splice(startIdx, count); // Modifies original // Summing an array const sum = nums.reduce((acu, num) => acu + num, 0);Retrieve elements and most common items from a Counter
Use theelements()method to return an iterator over elements repeating each as many times as its count. Usemost_common(n)to return a list of thenmost frequent elements and their counts as tuples.import collections counter = collections.Counter([1, 1, 2, 2, 3, 3, 3, 3]) # Get all elements as a list elements = list(counter.elements()) print(elements) # Output: [1, 1, 2, 2, 3, 3, 3, 3] # Get the 2 most common elements most_common = counter.most_common(2) print(most_common) # Output: [(3, 4), (1, 2)] (or similar depending on data)Implement client-side search with Fuse.js
For high-performance local search of data (such as LeetCode solutions) on the client side, use Fuse.js to index and query the dataset without requiring a backend search API.