patternjavaMinor
Registering and looking up aliases
Viewed 0 times
andaliaseslookingregistering
Problem
I've taken a coding challenge as part of a job interview and the recruiting process. Sadly I didn't get through it and couldn't secure the job. I am wondering if anyone can help me out here and show me how it could've been done better.
The problem described by interviewer was:
The purpose of the class is to register aliases for a value. For
example 'Dave, Davey and Davy are aliases for David" I'm looking
for working tests and some thought around edge cases, usability (i.e.
what that api would be like to use), and a reasonably efficient
implementation (does not need to be extremely high performance).
Thread safety is optional.
Provided skeleton:
```
public class Aliases {
public Aliases addAliases(String value, String... aliases) {
throw new UnsupportedOperationException();
}
public String lookup(String name) {
throw new UnsupportedOperationException();
}
}
public class AliasesTest {
private final Aliases nameAliases = new Aliases()
.addAliases("david", "dave", "davey", "davie", "davy")
.addAliases("thomas", "tom", "tommy")
.addAliases("michael", "mike", "micky")
.addAliases("elizabeth", "liz", "beth", "lizzie", "bettie", "lizbeth");
@Test
public void canLookupExactMatches() {
assertEquals("elizabeth", nameAliases.lookup("liz"));
assertEquals("david", nameAliases.lookup("davy"));
assertEquals("michael", nameAliases.lookup("michael"));
}
@Test
public void canLookupCaseInsensitiveMatches() {
assertEquals("elizabeth", nameAliases.lookup("Liz"));
assertEquals("david", nameAliases.lookup("DAVIE"));
assertEquals("michael", nameAliases.lookup("Mike"));
}
/ Add more test methods as required, feel free to remove/rename these if you prefer different terminology /
@Test
public void cannotFindLookupForAlias() {
// ....
}
@Test
public
The problem described by interviewer was:
The purpose of the class is to register aliases for a value. For
example 'Dave, Davey and Davy are aliases for David" I'm looking
for working tests and some thought around edge cases, usability (i.e.
what that api would be like to use), and a reasonably efficient
implementation (does not need to be extremely high performance).
Thread safety is optional.
Provided skeleton:
```
public class Aliases {
public Aliases addAliases(String value, String... aliases) {
throw new UnsupportedOperationException();
}
public String lookup(String name) {
throw new UnsupportedOperationException();
}
}
public class AliasesTest {
private final Aliases nameAliases = new Aliases()
.addAliases("david", "dave", "davey", "davie", "davy")
.addAliases("thomas", "tom", "tommy")
.addAliases("michael", "mike", "micky")
.addAliases("elizabeth", "liz", "beth", "lizzie", "bettie", "lizbeth");
@Test
public void canLookupExactMatches() {
assertEquals("elizabeth", nameAliases.lookup("liz"));
assertEquals("david", nameAliases.lookup("davy"));
assertEquals("michael", nameAliases.lookup("michael"));
}
@Test
public void canLookupCaseInsensitiveMatches() {
assertEquals("elizabeth", nameAliases.lookup("Liz"));
assertEquals("david", nameAliases.lookup("DAVIE"));
assertEquals("michael", nameAliases.lookup("Mike"));
}
/ Add more test methods as required, feel free to remove/rename these if you prefer different terminology /
@Test
public void cannotFindLookupForAlias() {
// ....
}
@Test
public
Solution
The first point in the feedback was
Code looks up values by walking the entire data-structure looking for a match (major).
If you turn your logic around you can fix this easily. The requirement is basically for a mapping alias => name. How about using the aliases as keys and the full name as the value? The code below also deals with another major feedback point also, storing the names in lower case, not doing the conversion on every lookup.
Code looks up values by walking the entire data-structure looking for a match (major).
If you turn your logic around you can fix this easily. The requirement is basically for a mapping alias => name. How about using the aliases as keys and the full name as the value? The code below also deals with another major feedback point also, storing the names in lower case, not doing the conversion on every lookup.
import java.util.Objects;
import java.util.Map;
import java.util.HashMap;
public class Aliases {
public Aliases addAliases(String value, String... aliases) {
for (String alias : aliases) {
this.aliases.put(alias.toLowerCase(), value);
}
this.aliases.put(value, value);
return this;
}
public String lookup(String name) {
Objects.requireNonNull(name, "Name must not be null");
return aliases.get(name.toLowerCase());
}
public static void main(String[] args) {
final Aliases nameAliases = new Aliases()
.addAliases("David", "dave", "davey", "davie", "davy")
.addAliases("Thomas", "tom", "tommy")
.addAliases("Michael", "mike", "micky")
.addAliases("Elizabeth", "liz", "beth", "lizzie", "bettie", "lizbeth");
String alias = "Liz";
System.out.printf("%s is an alias for %s%n", alias, nameAliases.lookup(alias));
alias = "dave";
System.out.printf("%s is an alias for %s%n", alias, nameAliases.lookup(alias));
}
// PRIVATE //
private Map aliases = new HashMap<>();
}Code Snippets
import java.util.Objects;
import java.util.Map;
import java.util.HashMap;
public class Aliases {
public Aliases addAliases(String value, String... aliases) {
for (String alias : aliases) {
this.aliases.put(alias.toLowerCase(), value);
}
this.aliases.put(value, value);
return this;
}
public String lookup(String name) {
Objects.requireNonNull(name, "Name must not be null");
return aliases.get(name.toLowerCase());
}
public static void main(String[] args) {
final Aliases nameAliases = new Aliases()
.addAliases("David", "dave", "davey", "davie", "davy")
.addAliases("Thomas", "tom", "tommy")
.addAliases("Michael", "mike", "micky")
.addAliases("Elizabeth", "liz", "beth", "lizzie", "bettie", "lizbeth");
String alias = "Liz";
System.out.printf("%s is an alias for %s%n", alias, nameAliases.lookup(alias));
alias = "dave";
System.out.printf("%s is an alias for %s%n", alias, nameAliases.lookup(alias));
}
// PRIVATE //
private Map<String, String> aliases = new HashMap<>();
}Context
StackExchange Code Review Q#120905, answer score: 5
Revisions (0)
No revisions yet.