cswork / libraryOpen practice workspace

Coding questions · Disney

Solve matrix add, frequency count, longest consecutive

This question evaluates proficiency in basic numerical and array operations (matrix addition), associative data structures for frequency counting (hash maps), and set-based algorithmic reasoning for the longest consecutive sequence problem.

You are asked to solve the following 3 coding tasks (in any language). Implement each as a function.

1) Add two matrices

Input: Two 2D integer arrays A and B, each of shape m x n.

Task: Return a new matrix C of shape m x n where C[i][j] = A[i][j] + B[i][j].

Assumptions:

  • A and B have the same dimensions.

Example:

  • A = [[1,2],[3,4]], B = [[5,6],[7,8]] → [[6,8],[10,12]]

2) Count occurrences of names

Input: A list/array of strings names.

Task: Return a dictionary / hash map from name → frequency (count of how many times it appears).

Example:

  • names = ["Amy","Bob","Amy"] → { "Amy": 2, "Bob": 1 }

3) Longest consecutive sequence

Input: An integer array nums (can contain duplicates and negative numbers).

Task: Return the length of the longest sequence of consecutive integers that can be formed from the elements of nums (order in the array does not matter).

Complexity requirement: Aim for O(n) time using a hash set (typical “Longest Consecutive Sequence” problem).

Examples:

  • nums = [100,4,200,1,3,2] → 4 (sequence 1,2,3,4)
  • nums = [0,3,7,2,5,8,4,6,0,1] → 9 (sequence 0..8)

Clarifications:

  • Duplicates should not inflate the sequence length.
  • If nums is empty, return 0.

Add Two Matrices

Return the elementwise sum of two same-shaped matrices.

Examples

Example 1

Input: ([[1,2],[3,4]], [[5,6],[7,8]])
Output: [[6, 8], [10, 12]]

Explanation: Add corresponding entries.

Example 2

Input: ([], [])
Output: []

Explanation: Empty matrices return empty output.

Hints

  1. Clarify edge cases before coding.
  2. Keep outputs deterministic when several valid answers exist.

Count Name Occurrences

Return a dictionary mapping each name to its frequency.

Examples

Example 1

Input: (["Amy","Bob","Amy"],)
Output: {'Amy': 2, 'Bob': 1}

Explanation: Amy appears twice.

Example 2

Input: ([],)
Output: {}

Explanation: No names means no counts.

Hints

  1. Clarify edge cases before coding.
  2. Keep outputs deterministic when several valid answers exist.

Longest Consecutive Sequence

Return the length of the longest consecutive integer sequence that can be formed from nums.

Examples

Example 1

Input: ([100,4,200,1,3,2],)
Output: 4

Explanation: The longest run is 1,2,3,4.

Example 2

Input: ([0,3,7,2,5,8,4,6,0,1],)
Output: 9

Explanation: The longest run is 0 through 8.

Hints

  1. Clarify edge cases before coding.
  2. Keep outputs deterministic when several valid answers exist.