SShortSingh.
Back to feed

LeetCode Two Sum: Dictionary Hashing Cuts Runtime from 2100ms to 0ms

0
·1 views

A developer tackled LeetCode's classic Two Sum problem, which asks for the indices of two numbers in an array that add up to a given target. The brute-force approach using nested for loops produced a working solution but ran slowly at 2100ms with 13.3MB of memory. A more efficient hashing-based solution using a Python dictionary achieved 0ms runtime while using slightly less memory at 12.9MB. The dictionary method works by storing previously seen values and checking on each iteration whether the required complement already exists. This comparison highlights how choosing the right data structure can dramatically improve algorithmic performance.

Read the full story at DEV Community

This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)

Log in to join the discussion and vote.

Log in

Related stories

0
ProgrammingDEV Community ·

How Reddit Handles Nested Comments and Front Page Rankings Efficiently

Reddit solves two core engineering challenges: storing arbitrarily deep comment trees and ranking posts by relevance over time. Rather than querying the database level by level, Reddit fetches all comments for a post in a single query and assembles the tree structure in application memory. For very deep threads, advanced models like materialized paths or closure tables can speed up subtree reads, though they add complexity to write operations. Post rankings use a time-decaying score that blends vote balance with submission time, giving newer quality posts a chance to rise while older ones gradually fall. This decay-based score is precomputed and only recalculated when votes change, keeping front page serving fast and efficient.

0
ProgrammingDEV Community ·

How Elasticsearch Finds Results in Milliseconds: Inverted Indexes and Shard Routing

Elasticsearch achieves fast full-text search by using an inverted index, which maps words to documents rather than scanning documents for words, enabling direct lookups instead of linear reads. At indexing time, documents are broken into tokens and stored in posting lists, allowing multi-word queries to quickly intersect or union sorted lists. To handle billions of documents, the index is split into shards — each a self-contained inverted index — distributed across multiple nodes so storage and query load are shared. A document is assigned to a shard by hashing its ID against the shard count, meaning reads by ID target a single shard, while full-text queries scatter to all shards and a coordinating node merges the ranked results. These design choices come with trade-offs: indexing is heavier than a simple database write, search is near-real-time rather than instant, and the shard count must be fixed early since changing it breaks the routing hash.

0
ProgrammingDEV Community ·

AI Coding Tools Like Cursor Can Introduce Prototype Pollution in Merge Functions

AI code editors such as Cursor frequently generate recursive object-merge helpers that contain a prototype pollution vulnerability, a developer warned in a recent post. The flaw allows a crafted JSON payload using a "__proto__" key to silently modify every object in a running application, potentially enabling privilege escalation. The vulnerability goes undetected during normal testing because it only triggers when malicious input is supplied. The root cause is that AI models learn from legacy blog posts and Stack Overflow answers that historically omit guards for special keys like "__proto__", "constructor", and "prototype". Developers can fix the issue by explicitly skipping those keys in merge loops, using Object.create(null) as the merge target, or catching the pattern early with static analysis tools such as Semgrep.

0
ProgrammingDEV Community ·

BDE Score: Open-Source Tool Rates Stocks Across US, Hong Kong, and China Markets

A developer has released BDE Score, a free open-source stock analysis tool that assigns each stock a composite rating between 0 and 100. The score is calculated using five weighted factors: momentum, volatility, volume, trend, and risk. The tool currently covers 74 stocks across US, Hong Kong, and A-Share Chinese markets in real time. Users can access data through a REST API without any account registration, and the full methodology is publicly available on GitHub. The project is intended for educational purposes and does not constitute financial advice.