Array Session 1

 First Session on Arrays


In this session, we will tackle two array problems ranging from easy to medium. Understanding how arrays work is essential for understanding most data structures and algorithms. Though arrays can seem like simple problems to solve they can be harder to solve at times.


When solving the problems think about what the time & space complexity of your solution is, The approach to solving most problems is first to use the brute force approach to get the solution then think about how you'd optimize your solution.


First Problem: Find the 1st duplicate value in the array?

you are given an array of integers between 1 and n where n is your array length. the goal of your function is to return the first integer in the array that appears more than once. (Note: assume the array is read from left to right)

If there are no repeated numbers your function should return -1

input: array = [2,1,5,2,3,3,4]

output: 2

input: array = [2,1,5,3,3,2,4]

output: 3

input: array = []

output: -1


Second Problem: Two Number Sum?

you are given an array and a target sum value, where two numbers in the given array added together equal the target sum. you are to assume that your function takes in a non-empty array of distinct integers.

The function should return the two numbers that make up the sum in an array or return an empty array if there are no numbers found in the array.


input: array = [5,5] target =10

output : [5,5] 

input: array = [1,2,3,4,5,6,7,8,9,15,20,40] target =18

output : [3,15] 

input: array = [1,2,3] target =22

output : [] 


Bonus Problem: Square Every Digit

Square every digit of a number and concatenate them. your function takes an integer and returns an integer.


input : 234

output: 4916

2^2 = 4, 3^2=9, 4^2=16


There are many similar problems online if you want to practice, you can also propose problems to solve for the next session on arrays.


Answers will be posted after the meeting, the meeting date will be announced next week.