Learnitweb

Author: Editorial Team

  • Why HashMap Allows One Null Key While Hashtable Doesn’t?

    1. Background Hashtable HashMap 2. Internal Working Difference Let’s look at how key lookup works in both: In HashMap In Hashtable 3. Design Rationale Aspect HashMap Hashtable Introduced in Java 1.2 (Collections Framework) Java 1.0 (Legacy class) Synchronization Non-synchronized Synchronized Null keys Allowed (one null key) Not allowed Null values Allowed Not allowed Performance Faster…

  • Checksum and How It Works

    Data integrity is one of the most critical aspects of computer systems, networking, and storage. Whenever data is transmitted or stored, there’s always a possibility that it might get corrupted due to noise, interference, or hardware failure.To detect such errors, a mechanism known as a checksum is used. This tutorial explains what a checksum is,…

  • Printing Even and Odd Numbers Using Two Synchronized Threads in Java

    1. Problem Description We want: Both should print numbers in sequence, i.e.,1 2 3 4 5 6 7 8 … (not random interleaving). To achieve this, we’ll make the two threads communicate and coordinate using wait() and notify() on a shared object lock. 2. Core Idea 3. Step-by-Step Logic This ensures alternating execution. 4. Complete…

  • How ExecutorService manage threads?

    1. Overview: What ExecutorService Really Is At a high level, ExecutorService is an abstraction built on top of thread pools.When you submit a task (a Runnable or Callable), the service does not create a new thread every time.Instead, it reuses a fixed set of worker threads that continuously fetch and execute tasks from a queue.…

  • LeetCode 451: Sort Characters By Frequency

    Problem Statement Given a string s, sort it in decreasing order based on the frequency of characters and return the resulting string. Example 1:Input: “tree”Output: “eert” or “eetr”Explanation: ‘e’ appears twice, ‘r’ and ‘t’ appear once each. Example 2:Input: “cccaaa”Output: “cccaaa” or “aaaccc”Explanation: ‘c’ and ‘a’ both appear three times. Order among same frequency characters…

  • LeetCode 1277: Count Square Submatrices with All Ones

    Problem Statement Given a m x n binary matrix mat (containing only 0s and 1s), return the total number of square submatrices that have all ones. Example 1:Input: Output: 15Explanation: Example 2:Input: Output: 7 Constraints: Approach in Plain English Key idea: DP stores size of largest square ending at each cell, which implicitly counts smaller…

  • LeetCode 230: Kth Smallest Element in a BST

    Problem Statement Given the root of a binary search tree (BST) and an integer k, return the kth smallest value (1-indexed) of all the values of the nodes in the tree. Example 1:Input: root = [3,1,4,null,2], k = 1Output: 1 Example 2:Input: root = [5,3,6,2,4,null,null,1], k = 3Output: 3 Constraints: Approach in Plain English Key…

  • LeetCode 901: Online Stock Span

    Problem Statement Design a class StockSpanner that collects daily stock prices and returns the span of the stock’s price for the current day. The span of the stock’s price today is defined as the maximum number of consecutive days (including today) the price of the stock was less than or equal to today’s price. Implement…

  • LeetCode 567: Permutation in String

    Problem Statement Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise. A permutation of a string is a rearrangement of its letters. For example, “ab” has permutations “ab” and “ba”. Example 1:Input: s1 = “ab”, s2 = “eidbaooo”Output: trueExplanation: “ba” is a permutation of “ab” and…

  • LeetCode 438: Find All Anagrams in a String

    Problem Statement Given two strings s and p, return all start indices of p’s anagrams in s. You may return the answer in any order. An anagram is a rearrangement of letters. For example, “abc” and “cab” are anagrams. Example 1:Input: s = “cbaebabacd”, p = “abc”Output: [0, 6]Explanation: Example 2:Input: s = “abab”, p…