Solve matrix add, frequency count, longest consecutive
Coding Tasks
You are asked to solve the following three coding tasks, implementing each as a function in your preferred language.
1) Add Two Matrices
Input: Two 2D integer arrays, A and B, both of shape m x n.
Task: Return a new matrix C of shape m x n where each element C[i][j] is the sum of the corresponding elements A[i][j] + B[i][j].
Assumptions:
AandBare guaranteed to 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 or array of strings, names.
Task: Return a dictionary (or hash map) where keys are the unique names from the input list and values are their respective frequencies (the count of how many times each name appears).
Example:
names = ["Amy","Bob","Amy"]→{ "Amy": 2, "Bob": 1 }
3) Longest Consecutive Sequence
Input: An integer array nums, which may 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. The order of elements in the input array does not matter.
Complexity Requirement: Aim for an O(n) time complexity solution, typically achieved using a hash set.
Examples:
nums = [100,4,200,1,3,2]→4(The sequence is1,2,3,4)nums = [0,3,7,2,5,8,4,6,0,1]→9(The sequence is0,1,2,3,4,5,6,7,8)
Clarifications:
- Duplicates in the input array should not inflate the sequence length.
- If
numsis empty, the function should return0.
AI interview prep
Core dilemma
Demonstrating foundational algorithmic proficiency, optimal data structure usage, and attention to detail across diverse problem types within a constrained time limit. The challenge is to not just solve, but to solve efficiently and robustly.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Solve matrix add, frequency count, longest consecutive
Coding Tasks
You are asked to solve the following three coding tasks, implementing each as a function in your preferred language.
1) Add Two Matrices
Input: Two 2D integer arrays, A and B, both of shape m x n.
Task: Return a new matrix C of shape m x n where each element C[i][j] is the sum of the corresponding elements A[i][j] + B[i][j].
Assumptions:
AandBare guaranteed to 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 or array of strings, names.
Task: Return a dictionary (or hash map) where keys are the unique names from the input list and values are their respective frequencies (the count of how many times each name appears).
Example:
names = ["Amy","Bob","Amy"]→{ "Amy": 2, "Bob": 1 }
3) Longest Consecutive Sequence
Input: An integer array nums, which may 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. The order of elements in the input array does not matter.
Complexity Requirement: Aim for an O(n) time complexity solution, typically achieved using a hash set.
Examples:
nums = [100,4,200,1,3,2]→4(The sequence is1,2,3,4)nums = [0,3,7,2,5,8,4,6,0,1]→9(The sequence is0,1,2,3,4,5,6,7,8)
Clarifications:
- Duplicates in the input array should not inflate the sequence length.
- If
numsis empty, the function should return0.