MIME Type puzzle discussion

Thank you so much!! This is the answer I was looking for!! Since creating and searching arrays is slow, I think the validation step counts how many arrays/lists you have and times you out if you added any.

For anyone else with the same issue:
I used C#, but the conclusion should be similar in other languages. My problem was that I created an extra array to split the file type by the last “.” just like the example did with " " (where it splits the 3rd input into input[0] and input [1] for the file extension and MIME type). There is no need for that extra array. I needed to figure out how to use a substring on the file name to access the last letters after the last “.” instead.

One reason might be that you created an extra array/list. Since creating and searching these are so slow, I think the validation step counts how many arrays/lists you have and times you out if you added any.

The conclusion should be similar in all languages, but I used C#. My problem was that I created an extra array to split the file type by the last “.” just like the example did with " " (where it splits the 3rd input into input[0] and input [1] for the file extension and MIME type). There is no need for that extra array. I needed to figure out how to use a different method for accessing the same info (in C# I used a substring on the file name to access the last letters after the last “.” instead).

You created an extra array. Since creating and searching these are so slow, I think the validation step counts how many arrays/lists you have and times you out if you added any.

The conclusion should be similar in all languages, but I used C#. My problem was that I created an extra array to split the file type by the last “.” just like the example did with " " (where it splits the 3rd input into input[0] and input [1] for the file extension and MIME type). There is no need for that extra array. I needed to figure out how to use a different method for accessing the same info (in C# I used a substring on the file name to access the last letters after the last “.” instead).

MANNNN This is the answer! Many Thanks!

For C#: Google “Dictionary” “ContainsKey()”

1 Like

Hi,
I’m very confused! Test 3 and 5 don’t pass and i don’t know why.
I console.error() all inputs and here is the unlogic things:

  • Test 3: the 11th input is “final.”
    so my solution return UNKNOWN but “application/pdf” is expected
  • Test 5: the 9999th input is “iLowerCaseAndUpperCaseLetters.”
    so my solution return UNKNOWN but “image/vnd.microsoft.icon” is expected

Each fail happened on the last input of the test and with a no extension input, just a “.”
So my UNKNOWN looks right.
Any idea to explain those fails?

UNKNOWN is the good answer in both cases, the error must be on a previous line.
Note that your answer is checked line by line, and that the verification stop on the first encountered error.
Btw you can check the tests content directly (which is useful for the large ones that can’t be displayed fully on the console) by clicking on this button:

3 Likes

Mh: on test4, if I paste its input in my IDE the output is correct but the online output is not.
I use an unordered map defined like this

using UnordMapCaseInsensitive = std::unordered_map<std::string, std::string, std::hash<std::string>, Comp>;

where

struct Comp
{
    bool operator() (const std::string& s1, const std::string& s2) const {
        if (s1.length() == s2.length())
        {
            std::string::size_type i = 0;
            for (const auto& c: s1)
            {
                if (std::tolower(s1[i]) != std::tolower(s2[i]))
                {
                    return false;
                }
                i++;
            }
            return true;
        }
        else
            return false; 
    } 
};

The online test outputs only the first file type correct and the rest is all “UNKNOWN”.
Weirdly enough, aside for the first file, for all the other files the map find method doesn’t find the key because the Comp operator is never called (I tried to put some cout inside the method).

very funny whats happening to me

Standard Output Stream:

UNKNOWN
UNKNOWN
UNKNOWN

Failure

Found:
Nothing
Expected:
UNKNOWN

Which case is that? Maybe you didn’t output an answer for one of the file names.

IMO the question is slightly wrong. The extension of .pdf is considered to be pdf, where in reality it is nothing.

1 Like

So far I have done this in C++, C#, and now C. For C++ and C#, they have maps/dictionaries which makes things easier. Since C doesn’t, I had to create 4 separate arrays based on the ASCII value for the first letter in the extension. My 4 arrays ranges were a2g, h2m, n2p, and q2z. The thought process is that by having one massive array it will time out going through the loops but by splitting into 4 sections even if the extension started with ‘Z’ it will only need to do 4 comparisons and then the chances of finding the extension greatly increase over having one array.
However, if anyone knows a better way to do it in C I am more than happy to hear how you did it.

Hve you solved that issue? If not then I can help you.

Last test is not very cool because it add a new case which are “file.yes.pdf”. It’s pretty boring to have an error in this case because it’s flooded by 9998 other strings.

Add a test with “file.yes.pdf” before the last one could make this exercise less frustrating

I wrote my own functions to create and manage a hash table. I did copy the algorithm for the hash value from a website, see below, but made the rest up as I went along.

#define FNV_OFFSET 14695981039346656037UL
#define FNV_PRIME 1099511628211UL

// Return 64-bit FNV-1a hash for key (NUL-terminated). See description:
// https://en.wikipedia.org/wiki/Fowler–Noll–Vo_hash_function
static uint64_t hash_key(const char* key) {
    uint64_t hash = FNV_OFFSET;
    for (const char* p = key; *p; p++) {
        hash ^= (uint64_t)(unsigned char)(*p);
        hash *= FNV_PRIME;
    }
    return hash;
}

It runs in effectively zero time.

Only problem is I’m getting a miss-match on the large data set at record 9998. The MIME it’s complaining about has 669 extensions leading to it, there are 382 files with one of those types and it’s not the record it’s stopping on. Can’t get all the debug, can’t automate running it to step through the files. Ahh well, that’s tomorrows problem.

Okay, was a silly error in setting the file extension when there was no extension. Time for the next puzzle.

Hello i did this in Java i have no issue with the tests but the last one.

I suppose that my test is not optimised enough if someone can help me by giving an advice or some hint i would appreciate that.

Does the console state your code times out, without any other error messages? What data structure do you use in your code, and how is it structured (e.g. does it always loop through all possibilities, or does it break whenever it finds a match)? It is better if you can briefly describe your code so that we may advise more specifically.

1 Like

hello 5D,

thx for your reply.
I use a loop that verify for each data wich extension it is.
it loop trough all possibilities and put the unknown if none is the one.

Hello cedric.c1. Could you answer the remaining questions too? That is,
Does the console state your code times out, without any other error messages?
What data structure does your code use?
Does your code break out of the loop when it finds a match?

Yes the codes time out at 3500 something try.
No others error messages .
Tried with the break when the good value is found same issue but i go up to 5000 now.
Sorry if i’m not clear enough can i send my code to you to go faster ?