Tech Off Thread

5 posts

Coding Interview Question

Back to Forum: Tech Off
  • User profile image
    Rhamilton

    An old co-worker uses this as his interview question and I'm curious about how you might solve it.

    "Given an array of integers, find the mean and the frequency"

    The mean is very simple, but the frequency part is where different methodologies can come in.  Feel free to show and explain your process.  There is no right or wrong way, it's just a test of your intelligence/creativity.  

    //TODO: Find the frequency
    
    int[] intArray
    
    int meanDivisor = intArray.Count();
    int meanToBeDivided = 0;
    
    foreach(int number in intArray)
    {
        meanToBeDivided += number;
    }
    
    var mean = meanToBeDivided / meanDivisor;
    

     

  • User profile image
    evildictait​or

    This isn't a great interview question, because it's not really about programming, it's about knowledge - namely what is the mean and the frequency.

    Instead, ask someone a question that encourages them to think about how to solve the question, like, write me a function that returns TRUE if the parameter is a prime number or not, or write me the backend to a login form for a website - and see whether they want to hash and salt and iterate the passwords, and whether they have a SQL injection in their code and so on.

  • User profile image
    spivonious

    Under pressue I'd probably use a Dictionary with the number as the key and the count as the value and build it as I looped through finding the mean.

    I'm sure there are better ways to do it though.

    And I'm generally against programming tests during an interview. I wouldn't be applying for the job if I didn't know how to program.

  • User profile image
    JohnAskew

    LINQ makes this easy.

    var mean = myIntArray.Average();

    var  duplicates =  from val in myIntArray group val by val into result where result.Count() > 1 select new { Item = val.Key, ItemCount = val.Count() };

    foreach(var i in duplicates){  Debug.WriteLine(string.Format("The word {0} has {1} number of occurrences ", i.Item, i.ItemCount)); }

    ---------------------------

    I find asking questions about technology is more valuable when interviewing a coder's skillset. Probably because I feel comfortable knowing what answers are relevant and revealing.

    Many interviewees find coding exercises too nerve-racking, even some who shouldn't be intimidated at all. I might save that for a leadership role interview. Not sure.

  • User profile image
    MasterPi

    @spivonious: An alternative depending on the question constraints is to just use an int array, treating the indices as the keys.

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.