Coding Pattern: Two Pointers

Overview It is not easy to summarize the pattern of Two Pointers, but most likely it is used for list and linked list and the required time complexity is O(N) - the underlying pattern allows us to use Two Pointers to go through the list once to get the results. Common usage: Linear Structure: Typically applied to a sorted array or linked list. Two pointers might move in the same direction or in opposite directions. Classic Patterns: a. Converging Pointers (often used in sorted arrays): - Start one pointer at the beginning (left) and another at the end (right). - Move them toward each other until they meet or until some condition is satisfied. - Example: Checking if a sorted array has two numbers that sum up to a target. b. Sliding Window: - Use two pointers to represent the start and end of a window, then “slide” the window through the array/sequence. - Example: Finding the longest substring without repeating characters. c. Fast and Slow Pointers (often used in linked lists): - One pointer moves twice (or more times) as fast as the other. - Useful for detecting cycles in a linked list (Floyd’s Cycle Detection Algorithm) or finding the middle element. Usage Scenarios: Finding a Pair with a Given Sum/Target: Given a sorted array, determine if there’s a pair that sums up to a target. Move the left and right pointers based on the sum comparison to the target. Removing Duplicates: Two pointers can be used to remove duplicates from a sorted array or linked list, with one pointer iterating through and another pointing to the last non-duplicate item. Palindrome Checking: To determine if a string or linked list is a palindrome, you can use two pointers moving from the two ends towards the center. Max/Min Subarray/Sublist: Using the sliding window variant, find the subarray with the maximum/minimum sum or other properties. Advantages: Efficiency: The two-pointer technique can sometimes convert a brute force solution with time complexity O(n^2) to a more efficient O(n) solution. Space: This method is in-place and typically uses O(1) extra space. Example Questions 88. Merge Sorted Array You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively. ...

October 12, 2023 · 8 min · 1620 words · Me

Coding Pattern: Divide & Conquer

Overview The Divide-n-Conquer strategy often employs recursion, as it relies on applying the same method to reduce the problem size by half and subsequently combining the outcomes for the ultimate solution. I view Divide-n-Conquer in a light similar to MapReduce, particularly when the task involves transformation. MapReduce breaks down a large problem into more manageable, independent sub-problems. Since each of these sub-problems operates autonomously, we can address them sequentially and still integrate their solutions. Key to this approach is ensuring the main problem can be independently segmented and the derived solutions can be seamlessly merged. ...

October 8, 2023 · 6 min · 1257 words · Me

Coding Pattern: Binary Search

Overview In one word, binary search is to search for a target in a sorted array. The idea is to shrink the search space to empty. It must be sorted because we can be sure how to shrink the search space and we normally reduce the search space by half so the time complexity is O(logN) where N is the size of entire search space. One common problem to understand Binary Search is how to identify the boundary of the search space. ...

October 8, 2023 · 11 min · 2233 words · Me

Build a home media server but automated

Intro In Route transmission to VPN container, I talked about how to download contents via VPN tunnel so we can get rid of some troubles. But that is far from enough for us to build a home media server which should work as Netflix and Hulu to us. We shouldn’t bother with the torrent and subtitle search. Once we add a show to our watch list, everything should be set up automatically. I will introduce the tools and necessary setup for me to build a home media server in the following sections. ...

September 16, 2023 · 3 min · 623 words · Me

Expose NAS Services with Att Gateway

Recently I changed my ISP to ATT to try their fiber, and they gave me the bgw320 as the gateway for the Internet service. And I have trouble connecting to my Synology’s services like Jellyfin. I suspect it has some conflicts with the network of these docker services running in Synology or the internet setup of Synology. Considering the configuration of docker network could be another rabbit hole, I didn’t go that route. I decided to connect the gateway with my own router and use the passthrough function in the gateway so I can do the port forwarding in my router. ...

September 16, 2023 · 1 min · 210 words · Me

FLP impossibility in plain language

Overview FLP impossibility is to prove there is no algorithm can really achieve totally correct consensus in asychronous system under assumption at most one process is faulty. The paper is very famous and also difficult to understand given the wording. This article is to explain FLP impossibility in a plain way. Aschronous System In FLP paper, there are some settings/assumptions made to describe an aschronous system which is used in the proof. This system has: ...

September 16, 2023 · 7 min · 1468 words · Me

gRPC tutorial - 1: Overview

What is gRPC gRPC is a high performance open-source freature-rich RPC (remote procedure calls) framework developed by google. “g” stands for many different meaning like green, good and etc. It is a protocal that allows a program to execute a procedure of another program located in other computer without the developer explicitly coding the details for the remote interaction gRPC is one kind of s2s call and widely used to replace REST API in the backend due to: ...

September 16, 2023 · 2 min · 362 words · Me

gRPC tutorial - 2: Enviroment Setup

Environment Setup I am using Gradle and Intellij Idea for this project (Maven setting can be found here). The Gradle setting is shown as following: plugins { id "com.google.protobuf" version "0.8.18" id "java" } group 'today.ihelio.grpc.tutorial' version '1.0-SNAPSHOT' sourceCompatibility = 15 repositories { mavenCentral() } dependencies { implementation 'junit:junit:4.13.1' testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1' implementation group: 'com.google.protobuf', name: 'protobuf-java', version: '3.21.4' runtimeOnly group: 'com.google.protobuf', name: 'protobuf-java-util', version: '3.21.4' implementation group: 'io.grpc', name: 'grpc-all', version: '1.45.1' runtimeOnly group: 'io.grpc', name: 'grpc-services', version: '1.48.0' implementation group: 'javax.annotation', name: 'javax.annotation-api', version: '1.3.2' } test { useJUnitPlatform() } sourceSets { main { java { srcDirs 'build/generated/source/proto/main/grpc' srcDirs 'build/generated/source/proto/main/java' srcDirs 'src/main/resources' } } } protobuf { protoc { artifact = 'com.google.protobuf:protoc:3.21.4' } plugins { grpc { artifact = 'io.grpc:protoc-gen-grpc-java:1.49.0' } } generateProtoTasks { all()*.plugins { grpc {} } } } jar { from { configurations.runtimeClasspath.findAll { duplicatesStrategy = DuplicatesStrategy.EXCLUDE it.name.endsWith(".jar") }.collect { println 'add ' + it.name zipTree(it) } } } targetCompatibility = JavaVersion.VERSION_15 After we have the proto files, then we could just build the project to complie proto files and generate essential stubs. ...

September 16, 2023 · 3 min · 463 words · Me

gRPC tutorial - 3: Unary Call

Unary call is very simple to understand, it is essentially a normal REST API call. Each request will get a single response from the server. In this project, we implement unary call to create books in our store. First, we need create book_message.proto which is a book pojo carrying the related info of book. Simply speaking, each message is pojo we are gonna use in our service. syntax = "proto3"; package book; option java_multiple_files = true; option java_package = "today.ihelio.grpc"; import "sample_message.proto"; import "image_message.proto"; message Book { enum Genre { UNKNOWN = 0; FICTION = 1; MYSTERY = 2; THRILLER = 3; HORROR = 4; HISTORICAL = 5; ROMANCE = 6; SCI_FICTION = 7; } string id = 1; string name = 2; string author = 3; uint32 publish_year = 4; double price = 5; string publication = 6; uint32 rating = 7; uint32 rating_count = 8; double avg_rating = 9; repeated Sample sample = 10; repeated Image image = 11; repeated Genre genre = 12; optional uint32 popularity = 13; } To implement a message, we need speficy: ...

September 16, 2023 · 7 min · 1312 words · Me

gRPC tutorial - 4: Client Streaming

We will implement a function to upload image when we create the books like the cover and something else. And we would split the image into chunks and we upload them chunk by chunk until all data are transfered. As we defined earlier in the proto, we could have multiple images for one book. So we will need a function called uploadImage and uploade all images one by one. Let’s start with the proto of image: ...

September 16, 2023 · 8 min · 1656 words · Me