Huffman Coding Challenge

Given an array ARR of integers containing 'N' elements where each element denotes the frequency of a character in a message composed of 'N' alphabets of an alien language, your task is to find the Huffman codes for these alphabets.

Ensure the following for Huffman codes:

1) All codes should be binary strings.
2) Each code can distinctly identify its corresponding character.
3) Minimize the total number of bits used for the message.

Example:

Input:
ARR = [1, 4, 2]
Output:
Possible Huffman Codes: ['10', '0', '11']
Explanation:

Other valid codes could be ['01', '1', '00'], ['00', '1', '01'], etc. Invalid codes include ['1', '0', '01'], ['1', '10', '0'] due to non-uniqueness or other constraints.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 104
  • 1 <= ARR[i] <= 104
  • Time Limit: 1 sec
Input Format:
The first line contains an integer 'T', the number of test cases.
The next 'T' lines contain two lines each: first 'N', the number of elements in 'ARR'; second 'N' space-separated integers representing the elements of 'ARR'.
Output Format:
The checker will print 1 if the Huffman codes generated are valid and adhere to the specified rules, else it will print 0.
Note:
You do not need to print anything. It is managed for you. Implement the solution function only.
AnswerBot
3mo

Implement a function to generate Huffman codes for characters based on their frequencies in an alien language message.

  • Use a priority queue to build the Huffman tree efficiently.

  • Assign '0' and '1' to l...read more

Help your peers!
Select
Add answer anonymously...

Josh Technology Group Intern interview questions & answers

An Intern was asked Q. Inorder Successor in a Binary Tree Given an arbitrary binary tree and a specific...read more
An Intern was asked Q. Unique Element In Sorted Array Nobita wants to impress Shizuka by correctly gues...read more
An Intern was asked Q. Huffman Coding Challenge Given an array ARR of integers containing 'N' elements ...read more

Popular interview questions of Intern

An Intern was asked Q1. Inorder Successor in a Binary Tree Given an arbitrary binary tree and a specific...read more
An Intern was asked Q2. Unique Element In Sorted Array Nobita wants to impress Shizuka by correctly gues...read more
An Intern was asked Q3. Huffman Coding Challenge Given an array ARR of integers containing 'N' elements ...read more
Josh Technology Group Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits