0% found this document useful (0 votes)
36 views3 pages

Filenamecomparator

The document describes a task to write a string comparison method that orders strings in an intuitive way for users. Specifically, it must order whole numbers within strings naturally, but also account for other factors that could result in an unintuitive sort order. Requirements include returning an integer based on sort order, handling all ASCII strings, using natural number ordering, and writing tests. The goal is to create a stable, efficient, readable, well-documented solution.

Uploaded by

as
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views3 pages

Filenamecomparator

The document describes a task to write a string comparison method that orders strings in an intuitive way for users. Specifically, it must order whole numbers within strings naturally, but also account for other factors that could result in an unintuitive sort order. Requirements include returning an integer based on sort order, handling all ASCII strings, using natural number ordering, and writing tests. The goal is to create a stable, efficient, readable, well-documented solution.

Uploaded by

as
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

FileNameComparator

Most string comparators in programming languages compare strings based on the


ASCII values of their individual characters.

As an example, consider the following (unsorted) array of strings:


[“test2.txt”, “test10.txt”, “test1.txt”]

If you sort this array using the type an ASCII-value based comparator as described
above, the result is the following:
[“test1.txt”, “test10.txt”, “test2.txt”]

However, if you were to create 3 files with these names in the same directory on
your computer and viewed the contents of the directory sorted by name using a file
manager application (such as the Windows Explorer or the Finder), you will notice
that they are actually sorted as follows:
[“test1.txt”, “test2.txt”, “test10.txt”]

This ordering is more intuitive to end-users because 10 is bigger than 2.

Your task is to write a method “compare” that compares two strings and orders
them in an intuitive manner. The prototype for the method in Java would be the
following:
int compare (String s1, String s2)

At minimum, the comparator you write must compare whole numbers in the string
using their natural ordering. A whole number is any sequence of numeric characters
(0-9) that occur consecutively. So, "1 2 10" would come before "1 10 2" (each of
these strings has 3 whole numbers and 2 is less than 10), but "1210" would come
after "1102" (each of these strings has 1 whole number and 1102 is less than 1210).

For the purposes of this problem, we will not consider any fractional numbers, so '.'
and '/' should just be treated as delimiters like any other character. That is to say,
"1/5" would come before "1/20" (each of these strings has 2 whole numbers that
are compared and 5 is less than 20). We will also not consider negative numbers,
meaning that ‘-‘ should be interpreted as a delimiter like any other character (“-1”
would come before “-5” because ‘-‘ is equal in both strings and 1 is less than 5).

Note that, while comparing numeric values in the string must be implemented as
outlined above, this is only the minimum requirement. The goal of this problem is to
write a comparator that enforces a “natural” sort ordering that is intuitive to end-
users. You should try to think of other things besides the ordering of numbers
within a string that may result in a sort order that would be unintuitive to end users.
This requirement is purposefully ambiguous — trying to figure out what cases may
be “unintuitive” and justifying your assertions is part of the problem.
Requirements:

- Returns an integer less than 0 if s1 is less than s2


- Returns 0 if and only if the input strings are identical
- Returns an integer greater than 0 if s1 is larger than s2
- Must be able to compare all valid ASCII strings
- Uses the string representation appropriate for the chosen programming
language
- Sort ordering imposed by the comparator orders strings in a manner that is
intuitive to end-users. The minimum requirement is that it must use the
natural ordering for whole numbers within a string.

Notes:

- You may assume that each input string will be no longer than 255 characters
- You may assume that all of the characters in the input strings will be 1-byte
ASCII printable characters in the range of 32 and 126 inclusive (that is, you
do not have to deal with Unicode, special character encodings or ASCII
control characters)

Evaluation:

- Correctness of the code, especially for corner cases


- Stability of the code. There should not be any valid inputs that causes the
code to crash or throw an exception.
- Efficiency. You should strive for a solution that is O(s1.length() +
s2.length()) in time and O(1) in space.
- Readability/cleanliness. The code should be easily readable by other
programmers and should use good programming style.
- Documentation. You don’t necessarily need full Javadoc-style
documentation for every method, but you should have comments that
explain any non-obvious code and at least an overview comment on your
“compare” method that describes how it compares the input (it doesn’t need
to describe the implementation details, but it should explain how ordering is
determined).
- Test coverage. You should write unit tests for the “compare” method that
verify its correctness.

Submitting your answer:

The assignment should only require 4 hours of total work, but for convenience to
you and your interviewer, you will be allowed 48. When the 48 hours are up, or you
are finished, please email us your solution. We recommend including a paragraph
or two describing how you would continue to improve your solution if you had
more time.

You might also like