-
Notifications
You must be signed in to change notification settings - Fork 0
/
FAANG Test.r
104 lines (88 loc) · 2.92 KB
/
FAANG Test.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# These are some of the exercises that the FAANG companies put to evaluate the DS with +18 years of experience (my experience).
#Exercise ONE (Example)
x<-seq(1:10)
faangTest1 <- function (x) {
if (x > 5) { print("poronga")}
else {
if (x == 4) {return(x*1/2)}
else {return(x**2)}}}
vec <- Vectorize(faangTest1, vectorize.args = "x")
vec(x)
# Replace NAs with the mean or median in an array (FAANG Feb 2022)
NAfunc <- function(x) {
na_index <- which(is.na(x))
mean_x <- mean(x, na.rm=T)
x[na_index] <- mean_x
return(x)}
df_clean <- apply(df,2,NAfunc)
# What are the prime numbers that appear in a number X (FAANG Feb 2022)
FactoresPrimos <- function(num) {
current <- num
ret.vals <- vector()
x <- 2
while (x <= num - 1){
while (current %% x == 0) {
current <- current / x
ret.vals <- c(ret.vals, x)
}
x <- x + 1
}
if (is.logical(ret.vals)) return(num) else return(ret.vals)
}
# Generate FIBONACCI numbers (FAANG Feb 2022)
Num_fibonacci <-function(x){
num <-c(0, 1)
i =2
for (i in i:(x-1)){
num[i+1] <-num[i]+num[i-1]
}
num <-num[1:length(num)]
return(num)
}
Num_fibonacci(x)
# Calculate the mean of each number in a vector (FAANG March 2022)
media <- function(x){
return(abs(x)/(2))
}
media(x)
# Generate a random sample, show the repeated numbers and calculate the sum, the max, and the min. (FAANG March 2022)
set.seed(1234)
df<-sample(1:100, 100, replace = TRUE)
df
df[duplicated(df)]
dfd<-df[duplicated(df)]
dfnd<-df[!duplicated(df)]
max(dfd)
min(dfd)
sum(dfd)
table(dfd)
sort(table(dfd))
# Generate the odd numbers from 1 to 100, calculate the square root of each one and add them. (FAANG March 2022)
x<-seq(1,100,by=2) #números impares
raiz <- function(x){
return(abs(x)**(1/2))
}
raiz(x)
sum(raiz(x))
# Note: some questions were about SQL (more focused on queries than on data management) and I also did it with R. Just an example:
library(sqldf)
data_Male<-sqldf("SELECT * FROM data WHERE Sex like 'Male' ")
# SOME OTHERS EXAMPLES OF TECHNICAL QUESTIONS (from leetcode, solution in R)
#Given two integers a and b, return the sum of the two integers without using the operators + and - (leetcode - Medium)
x<-7
y<-3
sum(x,y)
# Find the Duplicate Number (leetcode - Medium)
df<-sample(1:50, 50, replace = TRUE)
dfd<-df[duplicated(df)]
table(dfd)
sort(table(dfd))
# Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays (leetcode - Hard)
A <- matrix(c(10, 8, 5, 12, 15, 19), ncol = 2, byrow = TRUE)
B <- matrix(c(5, 3, 15, 6, 10, 12), ncol = 2, byrow = TRUE)
X<-(A+B)/2
X ---- # Note: If the distribution is symmetric then the mean is equal to the median and the distribution will have zero skewness.
# Given two integers dividend and divisor, divide two integers without using multiplication, division, and mod operator. (leetcode - Hard)
dividend = 10
divisor = 3
div(dividend,divisor)