id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

orion.a.smith@gmail.com apcsa-encryption > apcsaHashStringCollisions
prev  |  next  |  chance

Given an int hashcode and an int n, calculate and return in ascending order the Strings of length n that could produce that hashcode output, assembled into a single String (because you might not know how to use an ArrayList, a preferable structure for this type of output). This problem could involve an awful lot of iteration very quickly, since every additional 16-bit char to test multiplies the size of the possibility space by 2^16 (65,536). So, to make your life easier you will just check the visible ASCII subset of 32 (Space) to 126 (~). And, because CodingBat really doesn't like problems that take a long time to process, n will either be only 1 or 2 for all inputs. Return the set of options as a String with commas between the options.


Note that you do not actually have to re-implement the hashCode algorithm for Strings in Java, since if you have a String s you can just call
s.hashCode()
What, by the way, is the point of this exercise? Though Java's String hashCode algorithm is primitive and in no way cryptographically secure, an important consideration is whether a given hash output can be "reverse-engineered" into its possible input(s). This technique of checking for collisions can dramatically reduce the possibility space of inputs for a given hash output. Since passwords are, for example, often hashed in databases to prevent their discovery, the ease of finding a plaintext to produce a given output is a very important consideration.

HINT

It is definitely possible that there are zero Strings (of length n) which hash to a given output.

apcsaHashStringCollisions(2112, 2) → "Aa,BB,C#"
apcsaHashStringCollisions(2185, 2) → "Cl,DM,E."
apcsaHashStringCollisions(1514, 2) → "-w,.X,/9"

...Save, Compile, Run (ctrl-enter)

public String apcsaHashStringCollisions(int hash, int n) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 330

Copyright Nick Parlante 2017 - privacy