FastPrepDog Retrieval Precision and Recall
Problems/Dog Retrieval Precision and Recall

Dog Retrieval Precision and Recall

Microsoft logoMicrosoft● EasyNEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Microsoft new grad onsite interview. The report gives a task with ten dog images, precision and recall, and follow-ups for None or the wrong count. Here, we check the response, handle a zero denominator, and calculate from the actual count. Our version is about 85% the same as the reported question.

A corpus contains exactly ten image files, identified by 0 through 9. The entry labels[i] is 1 when file i depicts a dog and 0 for another animal. A retrieval API was asked for k files, and its received response is represented by returnedIds and responsePresent.

Return [precision, recall] using these rules:

  • If responsePresent is false, the response is None. Ignore returnedIds and return [-1.0, -1.0].
  • A present response containing a duplicate ID or an ID outside 0..9 is invalid. Return [-1.0, -1.0] for the whole response.
  • Otherwise, compute metrics from all actual returned files, even when their count differs from k. Precision is the number of returned dogs divided by the actual returned count. Recall is the number of returned dogs divided by the total number of dogs in the corpus.
  • Define a metric with denominator zero as 0.0. A present empty response is valid and differs from a missing response.

The received result is supplied as input; no image classifier or external API call is required. Absolute error of at most 10^-9 is accepted.

Function

dogRetrievalMetrics(labels: int[], returnedIds: int[], k: int, responsePresent: boolean) → double[]

Examples

Example 1

labels = [1,0,1,0,0,1,0,0,0,1]returnedIds = [0,1,5]k = 3responsePresent = truereturn = [0.6666666666666666,0.5]

Two of the three returned files are dogs, and the corpus contains four dogs.

Example 2

labels = [1,0,1,0,0,1,0,0,0,1]returnedIds = [0,1]k = 5responsePresent = truereturn = [0.5,0.25]

The API returned only two files despite requesting five. Precision uses the actual two results, while recall still uses all four corpus dogs.

Example 3

labels = [1,0,1,0,0,1,0,0,0,1]returnedIds = []k = 4responsePresent = falsereturn = [-1.0,-1.0]

The API response is None, represented by responsePresent=false. Metrics are unavailable rather than zero.

Constraints

  • labels.length = 10, and each label is 0 or 1.
  • 0 <= k <= 10.
  • 0 <= returnedIds.length <= 100; each received ID is between -100 and 100.
  • responsePresent is a boolean.

More Microsoft Onsite Interview questions

  • Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
  • Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
  • Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
  • Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
  • Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
  • Coin ChangeONSITE INTERVIEW · Seen Oct 2026
  • Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
  • Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • Count Index Pairs with Equal Value OffsetGoogle · ONSITE INTERVIEWGoogle interview questions
  • Net Position Calculation from API SnapshotsHeadlands Technologies · ONSITE INTERVIEWHeadlands Technologies interview questions
  • Square Each Array ValueAMD · ONSITE INTERVIEWAMD interview questions
  • Max Points on a LineLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Group-Relative Policy AdvantagesAmazon · ONSITE INTERVIEW
  • Product Total With the Cheapest PromotionInstacart · ONSITE INTERVIEWInstacart interview questions
  • Equation Tuple CombinationZoox · ONSITE INTERVIEW
  • Unique Near-Equal Target-Sum PairsFivetran · ONSITE INTERVIEWFivetran interview questions
  • Minimum Unique Card Suffix LengthBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
  • Count Pairs Divisible by KApple · ONSITE INTERVIEWApple interview questions

Interview reports that mention this question

  • Microsoft AI/ML Intern Interviews: 114 Experiences (Part 1 of 2)

Microsoft interview experiences

  • Microsoft OA Questions, October 2026: 4 Experiences
  • Microsoft Experienced SWE Phone Screen: 1 Experience
  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public double[] dogRetrievalMetrics(int[] labels, int[] returnedIds, int k, boolean responsePresent) {
        // Write your code here
    }
}
Checking account…