-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_test.php
More file actions
194 lines (176 loc) · 6.01 KB
/
debug_test.php
File metadata and controls
194 lines (176 loc) · 6.01 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
//Question #1: Why is this function bad? How do you fix this?
function loop_through ($j)
{
for ($i=0; $i < $j; $i++) {
echo $i;
}
}
//Question #2: Why is this function bad? How do you fix this?
function is_null_custom_function() {
$obj = null;
if ($obj == false) {
echo 'is not null';
} else {
echo 'is null';
}
}
//Question #3: Why is this function not working as expected? How do you correct this?
/**
* Test Output:
* divide_two_numbers (1, 2) => 0.5
* divide_two_numbers (10,0) => Division by zero
* Expected Output:
* divide_two_numbers (1,2) => 0.5
* divide_two_numbers (10, 0) => 0
*/
function divide_two_numbers ($x, $y)
{
return $x / $y;
}
//Question #4: During a large data migration, you get the following error:
/**
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 54 bytes).
You've traced the problem to the following snippet of code:
$stmt = $pdo->prepare('SELECT * FROM largeTable');
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($results as $result) {
// manipulate the data here
}
How would you refactor this code so that it stops triggering the memory error.
*/
//Question #5: What's wrong with this function?
//How do you correct this properly without changing return value?
/**
* Test Output:
* update_list_a_value (['a']) => true
* update_list_a_value (['b', 'c']) => true
* Expected Output:
* update_list_a_value (['a']) => true
* update_list_a_value (['b', 'c']) => false
*/
function update_list_a_value ($list)
{
$some_boolean_condition = true;
if (array_key_exists('a', $list))
{
$list['a'] = 'updated';
}
return $some_boolean_condition;
}
//Question #6: What's wrong with this function?
//We want to see this in a text document not HTML.
/**
* Test Output:
* seperate_string_with_commas (['a', 'b']) => a,b,
* seperate_string_with_commas (['a', 'b']) => a,
* Expected Output:
* seperate_string_with_commas (['a', 'b']) => a,b
* seperate_string_with_commas (['a']) => a
*/
function seperate_string_with_commas ($list) {
$final_string = '';
foreach ($list as $key => $value) {
$final_string = $final_string . $value . ',';
}
return $final_string;
}
//Question #7:
/**
Show some code that illustrates how you would hash a user's password, and also how you
would check that a password supplied by a user matches the password on file.
If your client cannot upgrade to PHP 5.5, and you have to hash passwords using existing
PHP libraries, what is one library/package that makes this easy?
*/
//Question #8: Why is this code bad? How would you improve it(HINT: best practice)?
/**
* Test Output:
* generate_dynamic_string ($x, $y, $z) => A non-numeric value encountered
* generate_dynamic_string (1,2,3) => Lorem Ipsum is simply dummy text of the printing
* and typesetting industry. Lorem Ipsum has been the industry's 1standard dummy
* text ever since the 1500s, when an unknown printer took a galley of type and
* scrambled2it to make a type specimen book. It has survived not only five
* centuries, 3but also the leap into electronic typesetting, remaining
* essentially unchanged. It was popularised in the 1960s with the release of
* Letra1set sheets containing Lorem Ipsum passages, and more recently with
* desktop publishing2software like Aldus PageMaker including versions of
* Lorem Ipsum.6
*/
function generate_dynamic_string ($x, $y, $z) {
$results = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. ' .
'Lorem Ipsum has been the industry\'s ' . $x . 'standard dummy text ever since the 1500s, when' .
' an unknown printer took a galley of type and scrambled' . $y . 'it to make a type specimen book. ' .
'It has survived not only five centuries, ' . $z . 'but also the leap into electronic typesetting, ' .
'remaining essentially unchanged. It was popularised in the 1960s with the release of Letra' . $x .
'set sheets containing Lorem Ipsum passages, and more recently with desktop publishing' . $y .
'software like Aldus PageMaker including versions of Lorem Ipsum.' . ($x * $y * $z);
return $results;
}
//Question #9: Why is the return value bad? How do you fix this(HINT: look at line 126)?
/**
* Test Output:
* return_json_data_of_scientist() => {"scientist":["Schr\u00f6dinger","Einstein","Newton"]}
* Test Expected:
* return_json_data_of_scientist() => {"scientist":["Schrödinger","Einstein","Newton"]}
*/
function return_json_data_of_scientist ()
{
$results = [
'scientist' => ['Schrödinger', 'Einstein', 'Newton']
];
return json_encode($results);
}
//Question #10: Why is the function output bad? How do you fix this (HINT: Best practice)?
function print_friend_list ()
{
$friends = ['Bob', 'Anna', "<script>alert('hello');</script>", 'Sally'];
echo '<ul>';
foreach ($friends as $friend) {
echo '<li>' . $friend . '</li>';
}
echo '</ul>';
}
//Question #11: Why is this function bad? How do you fix this?
/**
* Test Output:
* is_adult(5) => 'is child'
* is_adult(20) => 'is adult'
* is_adult(21) => 'is child'
* is_adult(19) => 'is child'
*/
function is_adult ($age)
{
if ($age = 20)
{
return 'is adult';
}
else
{
return 'is child';
}
}
//Question #12: Why is this function not working as expected? How do you correct this?
/**
* Test Output:
* get_plural_data ('abc') => [ ["plural"]=> "abcs" ["single"]=> "ab"]
* get_plural_data ('abcs') => [ ["plural"]=> "abcs" ["single"]=> "abc"]
* Expected Output:
* get_plural_data ('abc') => [ ["plural"]=> "abc" ["single"]=> "abc"]
* get_plural_data ('abcs') => [ ["plural"]=> "abcs" ["single"]=> "abc"]
*/
function get_plural_data ($model)
{
$model_plural = $model . 's';
$model_singular = $model;
if (strlen($model) > 0 && substr($model, -1) == 's') {
$model_plural = $model;
}
if ((strlen($model) > 0 && substr($model, -1)) == 's') {
$model_singular = substr($model, 0, strlen($model) - 1);
}
return [
'plural' => $model_plural,
'single' => $model_singular,
];
}