ridiculous interview i was asked

anyone have any idea how you would code this, code would be nice so i could see it but an explanation isnt discouraged

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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
 Write a function "void link(char **tiles)" that given a list of 'tiles' links them together sequentially in the order
they are given, and prints out its progress at each step. 

A tile is a square split into quadrants, where each quadrant has a number from 0 to 9. 

A tile can connect if at least two of its quadrants match quadrants on adjacent tiles, but all adjacent quadrants have to match. 

Tiles can be rotated, and the inputs are always designed so there is only one place a tile will fit. 

Examples: 
char *tiles1[] = {
	"11",     // 1
	"23",
	"",
	"44",     // 2
	"11",
	"",
	"16",     // 3
	"36",
	"",
	"51",     // 4
	"71",
	"",
	"46",     // 5
	"26",
	"",
	"14",     // 6
	"68",
	0};


link(tiles1) prints:

6 tiles
link 1
11
23
 
link 2
44
11
11
23
 
link 3
44
11
1116
2336
 
link 4
  44
5111
711116
  2336
 
link 5 // note this tile had to be rotated 180 degrees clockwise
  44
5111
71111662
  233664
 
link 6 // note this tile had to be rotated 270 degrees clockwise
  4448
511116
71111662
  233664
 
 


char *tiles2[] = {
	"12",	// 1
	"34",
	"",
	"36",	// 2
	"15",
	"",
	"56",	// 3
	"78",
	"",
	"78",	// 4
	"90",
	"",
	"46",	// 5
	"26",
	"",
	"88",	// 6
	"01",
	"",
	"93",	// 7
	"54",
	0};


link(tiles2) prints:

7 tiles
link 1
12
34

link 2 // note this tile had to be rotated 180 degrees clockwise
5112
6334

link 3 // note this tile had to be rotated 90 degrees clockwise
755112
866334

link 4 // note this tile had to be rotated 90 degrees clockwise
97755112
08866334

link 5 // note this tile had to be rotated 270 degrees clockwise
97755112
08866334
   66
   42

link 6 // note this tile had to be rotated 90 degrees clockwise
97755112
08866334
08 66
18 42

link 7 // note this tile had to be rotated 270 degrees clockwise
97755112
08866334
08 66 34
18 42 95




char *tiles3[] = {
	"11",     // 1
	"12",
	"",
	"12",     // 2
	"93",
	"",
	"93",     // 3
	"99",
	"",
	"39",     // 4
	"98",
	"",
	"95",     // 5
	"86",
	"",
	"57",     // 6
	"66",
	"",
	"77",     // 7
	"57",
	"",
	"13",     // 8
	"24",
	"",
	"45",     // 9
	"47",
	"",
	"24",     // 10
	"39",
	0};

link(tiles3) prints:

10 tiles
link 1
11
12
 
link 2
11
12
12
93
 
link 3
11
12
12
93
93
99
 
link 4
11
12
12
93
9339
9998
 
link 5
11
12
12
93
933995
999886
 
link 6
11
12
12
93
93399557
99988666
 
link 7
11
12
12    77
93    57
93399557
99988666
 
link 8
1113
1224
12    77
93    57
93399557
99988666
 
link 9
1113
122445
12  4777
93    57
93399557
99988666
 
link 10
1113
122445
12244777
9339  57
93399557
99988666
Last edited on
Sounds more like a homework problem than an interview question :P

Anyway I'd start by making a class for the field. It'd basically be a 2D array that's resizable on any side with a helper function to place new tiles.

To keep it simple I'd brute force the placement. Just go through each open spot on the field (with 2 extra slots on each side) and see if it's a legal place to put the tile. If it is, place it, if not, move on to the next place.

If no places match, rotate the tile, try again.
how would i handle the rotations though ?
by rotating the tile? =P

move the UR number to DR, the DR number to DL, the DL number to UL, and the UL number to UR.
can you provide a small snippet of something similar to what you explained but not exactly the answer so i have an idea of what you mean ?
A right rotation (not the most efficient but it is a snippet that does a rotation):
1
2
3
4
5
6
7
8
9
char tile[2][2];
// assume we have the layout
// [0][0] [0][1]
// [1][0] [1][1]
char temp = tile[0][0];
tile[0][0] = tile[1][0]; // DL to UL
tile[1][0] = tile[1][1]; // DR to DL
tile[1][1] = tile[0][1]; // UR to DR
tile[0][1] = temp; // UL to UR 

Just move the numbers as Disch said.
alright , now im confused on placement if the isnt a match of the number
Topic archived. No new replies allowed.