Coding challenges

By Mohamed Boucetta

Back to my home page

Coding image

Coding challenges are coding problems I will set weekly. I will give their solutions both in Swift and Java. You can download the code of all the solutions at Github.
If you are familiar with another language, you can learn twice by translating the code to your favorite language. The solution I am proposing are mine and can be optimized in term of space and time complexity. I will be happy to have your optimal solutions at my email adress: m.boucetta(at)uca.ac.ma.

List of problems

  1. Min and max of an array
    Build a function which takes an array of integers and returns its minimum and maximum. For instance, if the array is [3, 6, -1, -8, 78, -33], the function returns [-33, 78].
    Download the solution
  2. Swap two variables in place
    Write a function to swap the values of two variables in place (that is, without temporary variables). For instance, if variable1 = 6 and variable2 = 5, after calling the function, variable1 = 5 and variable2 = 6.
    Download the solution
  3. Matrix sorted by the sum of its rows
    Build a function which takes a matrix of integers with n rows and m columns and returns a matrix with the same rows but sorted by their sum. For intance if the input is [[0, 1, 2], [1, -1, 0], [7, 8, -9], [0, 3, 1]], the sum of the first row is 3, 0 for the second row, 6 for the third one and 4 for the last one. So the output is [[1, -1, 0], [0, 1, 2], [0, 3, 1], [7, 8, -9]].
    Download the solution
  4. Has a cross
    Build a function which takes a square matrix whose entries are 0, 1 or 2 and returns 1 if there is a row and a column of 1, returns 2 if there is a row and a column of 2 and returns 0 otherwise. For instance if M= [[1, 1, 1], [1, 0, 2], [1, 2, 0]], N= [[1, 0, 1], [1, 0, 2], [1, 2, 0]] and L = [[1, 2, 1], [1, 2, 0], [2, 2, 2]], for M the function will return 1, for N the function will return 0 and for L the function will return 2.
    Download the solution
  5. Pairs in an array with a given sum
    Build a function which takes an array of integers and a number and returns all the pairs in the array whose sum is the number. For instance if L=[1, 2, 4, 3, 0, 5] and the number is 5 the function returns [[2,3],[1,4],[0,5]]. If sum = 10, the function returns [[5,5]].
    Download the solution
  6. Binary search
    Build a function which take a sorted array of integers and a number and returns true if the number is in the array false otherwise. For instance, if L=[0, 5, 7, 10, 19], the function returns true if the number is 7 and false if the number is 6.
    Download the solution
  7. Peak of an array
    A peak of an array is an item array[i] such that array[i] >= array[i-1] and array[i] >= array[i+1]. Note that the first item array[0] is a peak if array[0] >= array[1] and the last item array[end] is a peak of array if array[end] >= array[end-1]. For instance, L=[2, 1, 4, 3, 7, 9, 10] contains three peaks 2, 4, 10.
    Build a function which takes an array of integers and returns a peak. Note that an array contains always a peak and can have many peaks.
    Download solution
  8. Peak of a matrix
    A peak of a matrix M of type (n,p) is an item M[i][j] such that M[i][j]>= M[i+1][j] (if M[i+1][j] exists), M[i][j]>= M[i-1][j] (if M[i-1][j] exists), M[i][j]>= M[i][j+1] (if M[i][j+1] exists) and M[i][j]>= M[i][j-1] (if M[i][j-1] exists). For instance, the matrix M=[[5, 6, 4, 3], [4, 8, 11, 8], [-1, 12, 7, 9]] has three peaks 11, 12, 9.
    Build a function which takes a matrix of integers of type (n,p) and returns a peak.
    Download solution

You can download the code of all the solutions at Github.

I give courses online via google meet, if you are interested, send me an email.

Contact

M. Boucetta
Faculty of sciences and technology
BP 549
Marrakesh Morocco
email: m.boucetta(at)uca.ac.ma