Skip to main content

Posts

Daily Coding Problem: Find the First Occurrence with Binary Search

Finding a value in a sorted array is a familiar binary-search problem. Finding its first occurrence adds a small but important requirement: when the middle value matches, you cannot stop. An earlier copy may still be waiting on the left. In this exercise, we will build one iterative solution in Python and Java, trace every boundary change, explain why it is correct, and test the cases that commonly cause off-by-one errors. The same idea also finds the insertion position for a missing value. The problem and input contract Given an integer array sorted in nondecreasing order and an integer target, return the zero-based index of the target's first occurrence. Return -1 when it is absent. Repeated values are allowed. The input must already be sorted, and the algorithm does not modify it. For [1, 2, 2, 2, 5] and target 2 , the answer is 1 , not 2 or 3. For the same array and target 4 , the answer is -1 . An empty array also returns -1 . Both implementations expect an array or lis...
Recent posts

Daily Coding Problem: Validate a Bracket String

Problem A string contains only (), [] and {}. Return whether every opening bracket closes in reverse order. Worked example {[()]} is valid. ([)] fails when ) meets [. Read {, [, and ( in that order. The most recent opener must close first, so ) removes (, ] removes [, and } removes {. For ([)], the first closer is ) while [ is still on top. Stop immediately. How the solution works The stack holds unmatched openers, with the most recent one on top. An empty stack at the end means no opening bracket remains. Python stores opening brackets. Java stores the closing bracket it expects, so the two versions enforce the same order. Counting opening and closing brackets alone misses crossed pairs like ([)]. Python solution def valid_brackets(text): closing = {')': '(', ']': '[', '}': '{'} stack = [] for char in text: if char not in closing: stack.append(char) elif not stack or stack.pop() != closing[char]: ...

Daily Coding Problem: Find Two Numbers That Add to a Target

Problem Given a list of integers and a target, return two distinct indices whose values add to the target. Return None in Python or null in Java if no pair exists. Worked example For [4, 1, 6, 2] with target 8, return (2, 3). Insert 4 at index 0 and 1 at index 1. At index 2, 6 needs 2, which has not appeared yet. At index 3, 2 needs 6, and the map returns index 2. How the solution works The map contains only earlier indices, so the first match gives two distinct indices. Every pair can be found when the algorithm visits its later index. Checking the map before insertion prevents a single 4 from being used twice when the target is 8. The Java version uses long for the complement so integer overflow cannot produce a false match. Python solution def two_sum(nums, target): seen = {} for index, value in enumerate(nums): complement = target - value if complement in seen: return seen[complement], index seen[value] = index return None Java soluti...

Mastering Zigzag Conversion: Transforming Text with Java and Python

Zigzag Conversion is a fascinating text transformation technique that adds visual appeal and complexity to encoded text. If you're a Java or Python enthusiast looking to explore this algorithm, you've come to the right place! In this blog post, we'll guide you through implementing Zigzag Conversion using Java and Python, providing you with the knowledge to transform text in a captivating zigzag pattern.   Understanding the Zigzag Conversion: The Zigzag Conversion algorithm is designed to rearrange characters in a string, creating a visually appealing zigzag pattern. It achieves this by organizing the characters in rows, following a specific pattern of upward and downward movements. The resulting transformed text can be an intriguing visual representation of the original input. How Does Zigzag Conversion Work? Let's dive into the mechanics of Zigzag Conversion. The algorithm takes two parameters: the input string and the number of rows in the zigzag pattern. It th...

Longest Palindromic Substring: Explained with Java and Python Code

The Longest Palindromic Substring problem is a classic challenge in the realm of string manipulation. In this blog, we will delve into the intricacies of finding the longest palindromic substring in a given string. Through a step-by-step approach and illustrative examples, you will gain a comprehensive understanding of the problem and learn how to solve it efficiently.   Understanding the Problem: A palindrome is a string that reads the same forward and backward. The Longest Palindromic Substring problem requires finding the longest substring within a given string that is also a palindrome. Our goal is to determine the length of this substring and return the substring itself.   Approach: To solve the Longest Palindromic Substring problem, we can employ a dynamic programming approach or use the expanding around center technique. Here, we will explore the dynamic programming approach. Create a boolean table, dp, of size [n][n] (where n is the length of the input string) to store ...