shlogg · Early preview
Prashant Mishra @prashantrmishra

There is always a price for those who persevere.

Validating A Binary Search Tree In O(n) Time Complexity

Validating a Binary Search Tree (BST) in O(n) time complexity. A BST is valid if for every node, its value is greater than all values in its left subtree and less than all values in its right subtree.

Designing A Trie Data Structure For Word Dictionary Operations

Implementing a Word Dictionary using Trie data structure. The `WordDictionary` class uses a Trie to store words and provides methods for adding words and searching for words with wildcards (`.`).

Minimizing Robot's Maximum Collectable Values In Grid Game

Minimax algorithm for grid game: TC O(n), SC O(n). Robot 1 tries to minimize values collected by Robot 2, who maximizes. Prefix sums used to optimize calculations.

Counting Stars Between Pipes In A String

Count Stars Between Pipes: Given a string s and ranges a, count stars enclosed between first & last pipes for each range. Use prefix & suffix arrays to find nearest pipes & calculate star counts.

Optimizing Vowel Count Queries In O(n) Time Complexity

Calculate prefix[] in O(n) and get vowel counts in O(k). Solution: iterate words, update prefix[], then calculate result for each query using prefix[]. Vowel check: a,e,i,o,u.

Calculating Subarray Sum Using Prefix Sum In O(1) Time Complexity

Calculate prefix sums in O(n) time, then use them to find subarray sums in O(1) time with NumArray class. Prefix[i] stores sum till ith index, making range sum calculation efficient.

Optimizing String Substring Count With Efficient Algorithm

Solution: Count substrings with at least k distinct characters. Use sliding window approach, incrementing right pointer and decrementing left pointer when necessary hash values reach threshold.

Designing Scalable Video Sharing Platform

Designing scalable video platform: Estimated 5B videos watched daily, 50GB storage needed. Using Amazon S3 & MongoDB for high availability & fast searches. Implementing CDN, cache & UDP/TCP protocols for efficient streaming.

Efficient Interval Intersection With O(nlogn) Time Complexity

Merge intervals from two lists in O(nlogn) time, where n is the total number of intervals. Use a single list to combine and sort both input lists, then greedily find overlapping intervals.

Software Engineering Rate Limiter Design Considerations

Rate limiters throttle users based on API requests in a given time period. Types include Hard, Soft, and Elastic throttling. Use cases include protecting systems from denial-of-service attacks and optimizing system bandwidth.

Optimizing Substring Count With Efficient Algorithm

Solution class counts valid substrings of word1 that can be formed using characters from word2, ensuring frequency of each character in the substring is not more than in word2.

Optimizing Binary Tree Node Robbery In O(n) Time Complexity

Robbing a binary tree: O(n) time complexity and space complexity using DP map and recursive stack space. Maximize node values by considering or not considering the current node's value.

Circular Sorting Algorithm For Array Elements

Circular sorting algorithm rearranges elements to their natural indices. It's useful for identifying duplicates and finding missing elements in an array. Example: [5,4,2,1,3] becomes [1,2,3,4,5].

Unique Paths Problem Solution In O(n*m) Time Complexity

Unique Paths problem solved using DP with TC: O(n*m) and SC: O(n*m) for dp array. Recursive function paths() returns number of unique paths from top-left to bottom-right.

Optimizing String Frequency With O(1) Time Complexity

Time complexity of frequencySort is O(n log n) due to priority queue operations, not O(1). It's a constant time complexity for inserting k elements into the priority queue where k ≤ 256.

Optimizing String Substring Length With O(n) Time Complexity

Optimal approach to find longest substring without repeating chars: TC O(n), SC O(256) using int[] of size 256. Update left pointer when duplicate found, keep track of max window substring.

Kadane's Algorithm For Maximum Subarray Sum

Kadane's Algorithm finds maximum sum of continuous subarray in O(n) time complexity and O(1) space complexity. It iterates through array, updating current sum and max sum as needed.

Behavioral Design Pattern: State Example With Java Implementation

The State design pattern changes a class's behavior based on its state. It uses an abstract state and concrete states to alter the context's behavior. Example in Java shows how it works with StartState and EndState classes.

Optimal Exor Of Range In O(1) Time Complexity

Optimal solution for finding XOR of range 1 to N: use N%4 to determine result in O(1) time complexity. For ranges L-R, find XOR of L-1 and R separately and XOR the results.

Single Number Problem Solution Using Bit Manipulation

Find the unique number in an array using bitwise XOR. TC: O(n), SC: O(1). SingleNumber(int[] nums) { int single = nums[0]; for(int i = 1; i < nums.length; i++) single ^= nums[i]; return single; }

Generating All Subsets Of An Array In O(2^n)*n Time Complexity

Backtracking approach: exponential time complexity, SC: O(2^n)*(n). Using Bit Manipulation: TC: O(2^n)*n, SC: O(2^n)*n. Generate subsets by considering bit values in nums[].

Reducing Memory Usage With Flyweight Pattern

Reducing memory usage by sharing data between similar objects is key to efficient software development. The Flyweight pattern achieves this by separating intrinsic & extrinsic state, improving performance & memory efficiency.

Command Pattern In Java: Wrapping Requests As Commands

Command pattern: a request wrapped in an object as command, passed to invoker, executed by corresponding object. Follows OCP & SIP solid principles. Example: stock market with BuyOrder & SellOrder classes.

Facade Design Pattern Simplifies Complex Systems

Facade design pattern hides system complexity, providing a simple interface for clients to interact with the system. It abstracts underlying complexities, making it easier to use and maintain.

Abstract Factory Pattern In UI Toolkit Development

Abstract Factory Pattern creates factories of factories, used when creating objects of different product families. It defines interfaces & concrete implementations for products & creators, ensuring correct UI components based on operating system.

Software Engineering: Interface Segregation Principle Explained

ISP prevents clients from depending on unused methods. Interfaces are segregated into smaller, single-responsibility interfaces like IPrint, IScan & IFax.

Breaking Inheritance Hierarchy With Liskov Substitution Principle

Objects should be replaceable with their subtype without affecting code correctness. Inheritance can lead to substitution failures, breaking hierarchy or using "Tell don't ask" principle can solve this issue.

Software Components Should Have One Responsibility Only

Software components should have 1 responsibility & 1 reason to change, not like Swiss-army knife which does many tasks. Example: Employee class modified to follow SRP.

Floyd Warshall Algorithm For Multisource Shortest Paths

Floyd Warshall algorithm finds shortest paths from all vertices to all others in a graph, detecting negative cycles. Time complexity: O(n^3), Space complexity: O(n^2).

Bellman Ford Algorithm For Negative Edge Weights And Cycles Detection

Bellman-ford algorithm finds single source shortest path in directed graphs with negative edge weights. It also detects negative cycles. Time complexity: O(n*m).

Dijkstra's Algorithm For Shortest Paths In Undirected Graphs

Dijkstra's single source shortest path algorithm finds shortest distances from one source to all other nodes in an undirected graph with non-negative edge weights. Time complexity: O(n+Elogn), space complexity: O(n).