PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates algorithmic problem-solving around aggregation and ranking, testing skills in computing aggregated relevance scores, deterministic tie-breaking, and efficient selection of top-k items.

  • medium
  • Microsoft
  • Coding & Algorithms
  • Software Engineer

Return Top K Relevant Apps

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

You are given a mapping from application name to keyword relevance scores, for example: ```json { "github": {"diff": 0.3, "code review": 0.8}, "jira": {"ticket": 0.7, "workflow": 0.4} } ``` Define an application's overall relevance as the sum of all its keyword scores. Given this structure and an integer `k`, return the names of the `k` most relevant applications sorted by descending relevance. If two applications have the same total relevance, break ties by application name in ascending lexicographic order. Discuss an efficient approach and its time complexity.

Quick Answer: This question evaluates algorithmic problem-solving around aggregation and ranking, testing skills in computing aggregated relevance scores, deterministic tie-breaking, and efficient selection of top-k items.

You are given a nested mapping where each application name maps to another mapping of keywords to relevance scores. The overall relevance of an application is the sum of all its keyword scores. Given this structure and an integer k, return the names of the k most relevant applications sorted by descending total relevance. If two applications have the same total relevance, break ties by application name in ascending lexicographic order. If k is larger than the number of applications, return all application names in the required order. If k is 0 or negative, return an empty list.

Constraints

  • 0 <= number of applications <= 10^5
  • 0 <= total number of keyword entries across all applications <= 2 * 10^5
  • k may be 0 or larger than the number of applications
  • Relevance scores are real numbers, and an application with no keywords has total relevance 0

Examples

Input: ({"github": {"diff": 0.3, "code review": 0.8}, "jira": {"ticket": 0.7, "workflow": 0.4}, "slack": {"chat": 0.5}}, 2)

Expected Output: ["github", "jira"]

Explanation: github and jira both have total relevance 1.1, which is higher than slack's 0.5. The tie is broken lexicographically, so github comes before jira.

Input: ({"zoom": {"meet": 0.5}, "asana": {"task": 0.5}, "notion": {"doc": 0.7}}, 2)

Expected Output: ["notion", "asana"]

Explanation: notion has the highest total relevance at 0.7. asana and zoom are tied at 0.5, and asana comes first lexicographically.

Input: ({"figma": {"design": 0.9}, "miro": {}}, 5)

Expected Output: ["figma", "miro"]

Explanation: figma has total relevance 0.9, while miro has no keywords and totals 0. Since k exceeds the number of applications, return all of them in ranked order.

Input: ({}, 3)

Expected Output: []

Explanation: There are no applications, so the result is an empty list.

Input: ({"alpha": {"x": 1.0}, "beta": {"y": 2.0}}, 0)

Expected Output: []

Explanation: When k is 0, no applications should be returned.

Hints

  1. First reduce each application to a single total score by summing its keyword relevance values.
  2. After computing totals, think about ordering pairs like (app_name, total_score) by total descending and name ascending; a size-k heap can avoid sorting everything.
Last updated: Jun 8, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Return Top K Open Businesses - Microsoft (hard)
  • Implement Memory Allocation and In-Memory Records - Microsoft (medium)
  • Implement K-Means and Detect Divisible Subarrays - Microsoft (medium)
  • Sort Three Categories In Place - Microsoft (medium)
  • Retain Top K Elements - Microsoft (medium)