Student Record Manager - Problem

You are tasked with building a Student Record Management System that can store and manage student information efficiently.

Each student record contains:

  • Name (string): Student's full name
  • ID (integer): Unique student identifier
  • Grades (array of integers): List of grades for different subjects

Your system should support the following operations:

  1. Add Student: Add a new student record to the system
  2. Search by ID: Find a student's complete record using their ID
  3. Calculate Average Grade: Compute the average of all grades for a specific student

For this problem, implement a function that takes a list of student records and a target student ID, then returns that student's average grade. If the student is not found, return -1.0.

Input & Output

Example 1 — Basic Student Record
$ Input: students = [["Alice",101,[85,90,78]], ["Bob",102,[92,88,95]]], targetId = 102
Output: 91.67
💡 Note: Student with ID 102 is Bob with grades [92,88,95]. Average = (92+88+95)/3 = 275/3 = 91.67
Example 2 — Student Not Found
$ Input: students = [["Alice",101,[85,90,78]], ["Bob",102,[92,88,95]]], targetId = 999
Output: -1.0
💡 Note: No student has ID 999, so return -1.0 to indicate student not found
Example 3 — Single Grade
$ Input: students = [["Charlie",103,[95]]], targetId = 103
Output: 95.0
💡 Note: Student 103 has only one grade [95], so average is 95.0

Constraints

  • 1 ≤ students.length ≤ 1000
  • 1 ≤ student ID ≤ 106
  • 1 ≤ grades.length ≤ 10
  • 0 ≤ grade ≤ 100

Visualization

Tap to expand
INPUTSEARCH ALGORITHMRESULTStudent Records Array[[101,85,90,78], [102,92,88,95]]Target ID: 102Find student with ID 102and calculate average grade1Check Student 1012ID 101 ≠ 102, continue3Check Student 1024Match! Calculate average:(92 + 88 + 95) / 3 = 91.67Average Grade91.67Student 102 foundReturn calculated averageor -1.0 if not foundKey Insight:Single-pass search with immediate calculation is more efficient than separate find and calculate phases.TutorialsPoint - Student Record Manager | Optimized Single-Pass Search
Asked in
Amazon 35 Microsoft 28 Google 22 Apple 18
23.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen