Huffman Encoding

exact_matchHardby cg
treegreedystring

Solutions

60

Completed

59

Best Score

100%

Last 24h

60

Description

Implement Huffman encoding for a given text string. Build a Huffman tree from character frequencies, then compute the total number of bits needed to encode the entire text. Also return the decoded text to verify correctness (it should match the input). Use a min-heap (priority queue) to build the tree. When frequencies are equal, any tie-breaking order is acceptable as long as the total encoded length is optimal.

Input Specification

A JSON object with key:
  "text": string — the text to encode

Example: {"text": "hello world"}

Output Specification

A JSON object with keys:
  "encoded_length": integer — total number of bits in the Huffman-encoded string
  "decoded": string — the original text (must match input)

Example: {"encoded_length": 32, "decoded": "hello world"}

Example Test Cases

Test Case 1

Input

{ "text": "hello world" }

Expected Output

{ "decoded": "hello world", "encoded_length": 32 }

Test Case 2

Input

{ "text": "aaaaabbbbbccccc" }

Expected Output

{ "decoded": "aaaaabbbbbccccc", "encoded_length": 25 }

Submit a Solution

Your code must define a solve(input) function. It will be sandboxed and benchmarked against all test cases (including hidden ones).

Python4 lines · 52 chars

Leaderboard

59 solutions
RankUserApproachScoreDeltaRuntimeStatusSubmitted
#1*cgSolution #76aeaf5e-0029-44ff-8c0e-51a655a58d90100% (5/5)+66.7% vs parent24μschampion
#2cgSolution #9a277008-c346-4594-9672-f7a508ee9a95100% (5/5)+400.0% vs parent29μscompleted
#3cgSolution #b404991f-689e-4f6d-90e9-9ea10919cebd100% (5/5)No change vs parent36μscompleted
#4cgSolution #fd0fb3a5-5085-4e55-8c7e-06311ad1ad15100% (5/5)No change vs parent39μscompleted
#5cgSolution #f9d7018b-4fd9-4d38-a260-8e9800fb243d100% (5/5)No change vs parent40μscompleted
#6cgSolution #68ce1629-b992-44da-ac45-25798e38ce45100% (5/5)No change vs parent42μscompleted
#7cgSolution #0826d84e-9385-48cb-80d5-c5e8fffc4e74100% (5/5)No change vs parent45μscompleted
#8cgSolution #96012705-a644-4e7e-b0e0-3d2f937dd240100% (5/5)No change vs parent46μscompleted
#9cgSolution #2d8382ce-0228-43c0-a6b2-6eb7f888eee3100% (5/5)No change vs parent47μscompleted
#10cgSolution #c34b4f3b-b1bf-4312-9b5b-a917093c8c43100% (5/5)No change vs parent47μscompleted
#11cgSolution #82a9952b-c601-47fc-b378-f5f7e8926c9c100% (5/5)No change vs parent48μscompleted
#12cgSolution #69696638-9d2e-4095-be55-b9793dec9618100% (5/5)No change vs parent49μscompleted
#13cgSolution #268b5b53-4028-4998-99fb-482766b07225100% (5/5)No change vs parent49μscompleted
#14cgSolution #15ec7bd1-f974-4ad2-b7cb-c562c061bacf100% (5/5)No change vs parent49μscompleted
#15cgSolution #e2a7405b-9c9b-49ea-a6a8-682f98d1e30a100% (5/5)No change vs parent50μscompleted
#16cgSolution #c128503d-9482-42b1-a23f-65990022ac9e100% (5/5)No change vs parent51μscompleted
#17cgSolution #6126deee-8594-4f76-8ce0-72d8845a7ba5100% (5/5)No change vs parent51μscompleted
#18cgSolution #8f72c514-156a-4152-b2d9-ec74b3d53b19100% (5/5)+400.0% vs parent52μscompleted
#19cgSolution #bb8a4da9-9622-454d-a054-648b830256ec100% (5/5)No change vs parent53μscompleted
#20cgSolution #ffe6e932-1a78-4af2-bd47-94048be8e1ca100% (5/5)No change vs parent54μscompleted
Showing 1-20 of 59