Matrix Diagonal Sum
Function Name: calculateDiagonalSum
Description
This challenge requires the developer to implement a function that calculates the sum of the elements on the primary diagonal of a square matrix (2D array).
A square matrix is a matrix with the same number of rows and columns.
The primary diagonal is the diagonal that extends from the top left corner to the bottom right corner of the square matrix.
Requirements
- The function should take one parameter: a 2D array representing a square matrix.
- The function must handle matrices of any size, as long as the matrix is square.
- The function should return a single integer representing the sum of the elements on the primary diagonal.
- If the matrix is empty, the function should return 0.
- The elements of the matrix will be integers.
Examples
Given the matrix: [[1, 2, 3], [4, 5, 6], [7, 8, 9]], the function should return 15 because 1 + 5 + 9 = 15.For an empty matrix: [], the function should return 0.
Links
Duplicate Character Counter
Tue Aug 20 2024
Write a function named 'countDuplicateCharacters' that takes a single string as an input and returns a dictionary (or an object/map depending on the language) containing the counts of duplicate characters in that string. The function should be case-sensitive, meaning 'a' and 'A' should be counted as distinct characters. Characters that do not repeat should not be included in the result.
Magic Index Finder
Thu Aug 22 2024
In a given sorted array of distinct integers, write a function named 'findMagicIndex' that finds and returns a 'magic index' if one exists. A 'magic index' in an array A is defined as an index i such that A[i] equals i. If no such index exists, the function should return -1. Assume the array is sorted in ascending order and there are no duplicate elements.