-
Notifications
You must be signed in to change notification settings - Fork 0
/
sessions.html
723 lines (643 loc) · 25.7 KB
/
sessions.html
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
<!DOCTYPE HTML>
<!--
Massively by HTML5 UP
html5up.net | @ajlkn
Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
-->
<html>
<head>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'UA-152751847-1');
</script>
<title>Questions & Solutions</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<link rel="stylesheet" href="assets/css/main.css" />
<noscript><link rel="stylesheet" href="assets/css/noscript.css" /></noscript>
</head>
<body class="is-preload">
<!-- Wrapper -->
<div id="wrapper">
<!-- Header -->
<header id="header">
<a href="index.html" class="logo">Questions & Solutions</a>
</header>
<!-- Nav -->
<nav id="nav">
<ul class="links">
<li><a href="index.html">Codeavor</a></li>
<li class="active"><a href="sessions.html">Questions & Solutions</a></li>
<li><a href="contact.html">About</a></li>
<!-- <li><a href="elements_copy.html">Style Guide</a></li> -->
</ul>
<ul class="icons">
<li><a href="#" class="icon brands fa-twitter"><span class="label">Twitter</span></a></li>
<li><a href="#" class="icon brands fa-facebook-f"><span class="label">Facebook</span></a></li>
<li><a href="#" class="icon brands fa-instagram"><span class="label">Instagram</span></a></li>
<li><a href="#" class="icon brands fa-github"><span class="label">GitHub</span></a></li>
</ul>
</nav>
<!-- Main -->
<div id="main">
<!-- Post -->
<section class="post">
<header class="major">
<span class="date">February 10, 2020</span>
<h3>Discussion Session</h3>
</header>
<h5>Topic:</h5>
<p>C++ in a UNIX Environment</p>
<header class="major">
<span class="date">January 27, 2020</span>
<h3>Advanced Session<br>
Lead - Harsha</h3>
</header>
<h5 style="display: inline;">Starter code</h5>
<!-- Show/Hide buttons -->
<ul style="display: inline;" class="actions small">
<li style="display: inline;"><a id="jan-27-starter-btn" onclick='displayCodeSnippet("jan-27-starter","jan-27-starter-btn");return false;' class="button small">Show</a></li>
<li><br></li>
</ul>
<div id="jan-27-starter" style="display: none;">
<a href="https://github.com/harshasrikara/DataStructures">link to GitHub</a>
<p></p>
</div>
<h5>Problem:</h5>
<p>Given a node that represents the head of a linked list, find where there exists a circular loop. Note: The linked list need not necessarily wrap to back to the head node.</p>
<h5 style="display: inline;">Solution: O(max(N,M)) </h5>
<!-- Show/Hide buttons -->
<ul style="display: inline;" class="actions small">
<li style="display: inline;"><a id="jan-27-solution-btn" onclick='displayCodeSnippet("jan-27-solution","jan-27-solution-btn");return false;' class="button small">Show</a></li>
<li><br></li>
</ul>
<div id="jan-27-solution" style="display: none;">
<pre><code>
public static boolean isCircular(Node node) {
Node pointer1 = node;
Node pointer2 = node;
while (pointer1 != null || pointer2 != null || pointer1 != pointer2) {
pointer1 = pointer1.getNextNode();
pointer2 = pointer2.getNextNode();
if (pointer2 != null) {
pointer2 = pointer2.getNextNode();
}
}
return false;
}
</code></pre>
</div>
<h5>Problem:</h5>
<p>Given two nodes that represent that heads of two different linked lists, identify whether the linked lists are connected at any point. </p>
<h5 style="display: inline;">Solution: O(max(M,N)) </h5>
<!-- Show/Hide buttons -->
<ul style="display: inline;" class="actions small">
<li style="display: inline;"><a id="jan-27-2-solution-btn" onclick='displayCodeSnippet("jan-27-2-solution","jan-27-2-solution-btn");return false;' class="button small">Show</a></li>
<li><br></li>
</ul>
<div id="jan-27-2-solution" style="display: none;">
<pre><code>
public static boolean isConnected(Node node, Node node2) {
if(isCircular(node) || isCircular(node2)) {
return false;
}
Node pointer1 = node;
Node pointer2 = node2;
while(pointer1.getNextNode() != null) {
pointer1 = pointer1.getNextNode();
}
while(pointer2.getNextNode() != null) {
pointer2 = pointer2.getNextNode();
}
if(pointer1.equals(pointer2)) {
return true;
}
return false;
}
</code></pre>
</div>
<header class="major">
<span class="date">November 21, 2019</span>
<h3>Advanced Session<br>
Lead - Harsha</h3>
</header>
<h5>Problem:</h5>
<p>Given a two dimensional ArrayList of boolean values that represent a maze where <strong>false</strong> represents a wall and <strong>true</strong> represents an open space, write a function that returns whether a path exists from the start to the end. Assume index (0,0) is the start of the maze and (n,n) represents the end. Assume that there only exists one path from start to end. Use the following code to initialize your arrayList. Diagonals do not count as a path.<br> </p>
<h5 style="display: inline;">Starter code for ArrayList </h5>
<!-- Show/Hide buttons -->
<ul style="display: inline;" class="actions small">
<li style="display: inline;"><a id="nov-21-starter-btn" onclick='displayCodeSnippet("nov-21-starter","nov-21-starter-btn");return false;' class="button small">Show</a></li>
<li><br></li>
</ul>
<div id="nov-21-starter" style="display: none;">
<pre><code>
List< List< Boolean>> list = new ArrayList<>() {
{
add(new ArrayList<>() {
{
add(true);
add(false);
add(false);
add(false);
add(true);
}
});
add(new ArrayList<>() {
{
add(true);
add(true);
add(true);
add(true);
add(true);
}
});
add(new ArrayList<>() {
{
add(false);
add(true);
add(false);
add(false);
add(true);
}
});
add(new ArrayList<>() {
{
add(true);
add(true);
add(false);
add(false);
add(false);
}
});
add(new ArrayList<>() {
{
add(false);
add(true);
add(true);
add(true);
add(true);
}
});
}
};
</code></pre>
</div>
<h5 style="display: inline;">Solution: Recursive O(MN) </h5>
<!-- Show/Hide buttons -->
<ul style="display: inline;" class="actions small">
<li style="display: inline;"><a id="nov-21-solution-btn" onclick='displayCodeSnippet("nov-21-solution","nov-21-solution-btn");return false;' class="button small">Show</a></li>
<li><br></li>
</ul>
<div id="nov-21-solution" style="display: none;">
<pre><code>
public static boolean doesPathExist(List< List< Boolean>> list, int i, int j) {
if(i < 0 || i >= list.size()) {
return false;
}
if(j < 0 || j >= list.get(i).size()) {
return false;
}
if(!list.get(i).get(j)) {
return false;
}
if(i == list.size()-1 && j == list.get(i).size()-1) {
return true;
}
List< Boolean> lst = list.get(i);
lst.set(j, false);
list.set(i, lst);
boolean left = doesPathExist(list, i, j-1);
boolean right = doesPathExist(list, i, j+1);
boolean up = doesPathExist(list, i-1, j);
boolean down = doesPathExist(list, i+1, j);
return left || right || up || down;
}
</code></pre>
</div>
<header class="major">
<span class="date">November 14, 2019</span>
<h3>Advanced Session<br>
Lead - Atharv</h3>
</header>
<h5>Activity: The Pie of Life</h5>
<ol><span class="image right"><img src="images/pieoflife.png" alt="" /></span>
<li>Draw one large circle on a piece of paper to represent a typical day in your life</li>
<li>Divide the circle into different slices</li>
<li>Each slice represents the amount of time you spend doing different activities</li>
<li>Ex: Sleeping, playing an instrument, studying, hanging with friends, physical activity </li>
<li>Share and guess which pie belongs to whom</li>
</ol>
<h5>Problem:</h5>
<p>Given <em>n</em> non-negative integers <em>a1, a2, ..., an</em>, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. <br>Note: You may not slant the container and n is at least 2. <br> </p>
<span class="image fit"><img src="images/water.jpeg" alt="" /></span>
<!-- <p>2. Say you have an array for which the <em>ith</em> element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times). <br>
Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).</p>
<span class="image fit"><img src="images/water2.jpg" alt="" /></span> -->
<h5 style="display: inline;">Solution: O(n<sup>2</sup>)</h5>
<ul style="display: inline;" class="actions small">
<li style="display: inline;"><a id="nov-14-solution-slow-btn" onclick='displayCodeSnippet("nov-14-solution-slow","nov-14-solution-slow-btn");return false;' class="button small">Show</a></li>
<li><br></li>
</ul>
<div id="nov-14-solution-slow" style="display: none;">
<pre><code>
/**
* Use two for loops to iterate through every possible combination of indicies
* to find the maximum area in O(n^2)
*
* @param array of numbers that represent height
* @return maxArea that can be held by the container
*/
public static int getMaxAreaSlowly(int[] array) {
int maxArea = 0;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
int height1 = array[i];
int height2 = array[j];
int area = Math.min(height1, height2) * (Math.abs(j - i));
if (area > maxArea) {
maxArea = area;
}
}
}
return maxArea;
}
</code></pre>
</div>
<h5 style="display: inline;">Solution: O(N) </h5>
<ul style="display: inline;" class="actions small">
<li style="display: inline;"><a id="nov-14-solution-fast-btn" onclick='displayCodeSnippet("nov-14-solution-fast","nov-14-solution-fast-btn");return false;' class="button small">Show</a></li>
<li><br></li>
</ul>
<div id="nov-14-solution-fast" style="display: none;">
<pre><code>
/**
* Two pointer solution that finds maximum area in O(n)
* Move the pointer that has the short height
*
* @param array of numbers that represent height
* @return maxArea that can be held by the container
*/
public static int getMaxArea(int[] array) {
int startIndex = 0;
int endIndex = array.length - 1;
int maxArea = 0;
while (startIndex != endIndex) {
int height1 = array[startIndex];
int height2 = array[endIndex];
int minimumHeight = Math.min(height1, height2);
int area = minimumHeight * (endIndex - startIndex);
if (area > maxArea) {
maxArea = area;
}
if (height1 > height2) {
endIndex--;
} else {
startIndex++;
}
}
return maxArea;
}
</code></pre>
</div>
<header class="major">
<span class="date">November 12, 2019</span>
<h3>Novice Session<br>
Leads - Ifrit & Harsha</h3>
</header>
<h5>Activity: Who Knows Who Best?</h5>
<p>Split in 2 teams. Every person on the other team has 30 seconds to write down their answer on a piece of paper. Crumble the paper up. The other team tries to guess who wrote what answer down as a team by writing down their guesses on the whiteboard. After guesses are made for the round the answers are revealed. At the end of every round the points are tallied up. After 5 rounds, whichever team has the most amount of points win.
</p>
<h5>Tutorial: Time & Space Complexity</h5>
<div class="iframe-container">
<iframe id="nov-12-presentation" src="https://docs.google.com/presentation/d/e/2PACX-1vQZqrWP9zonKuhl57ZFL1rZ6kNACqqLszqwHdmMJCc0V6AFaJZNNzXIor0Z7iWc-MfKogPk4NTVR9Yw/embed?start=false&loop=false&delayms=3000" allowfullscreen="true" mozallowfullscreen="true" webkitallowfullscreen="true"></iframe></div>
<p></p><h5>Problem:</h5>
<p>Given a list of Integers with n elements and a target, find the set of factors that multiply to the target. Challenge: And if none exist then provide the closest.</p>
<pre><code>
public static void findMultiple(List< Integer> list, int target) {
}
</code></pre>
<h5 style="display: inline;">Solution: O(N<sup>2</sup>)</h5>
<ul style="display: inline;" class="actions small">
<li style="display: inline;"><a id="nov-12-solution-slow-btn" onclick='displayCodeSnippet("nov-12-solution-slow","nov-12-solution-slow-btn");return false;' class="button small">Show</a></li>
<li><br></li>
</ul>
<div id="nov-12-solution-slow" style="display: none;">
<pre><code>
/**
* O(n^2) solution by brute force checking all combinations by iterating through two for loops
*
* @param list contains the list of all numbers
* @param target is the value that the factors need to multiply out to or be the closest to
*/
public static void findClosestFactor(List< Integer> list, int target) {
int num1 = 0;
int num2 = 0;
int diffVal = Integer.MAX_VALUE;
boolean exactMatch = false;
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < list.size(); j++) {
if (j != i) {
int multiple = list.get(i) * list.get(j);
if (Math.abs(target - multiple) < diffVal) {
diffVal = Math.abs(target - multiple);
num1 = list.get(i);
num2 = list.get(j);
}
if (multiple == target) {
System.out.println(list.get(i) + " * " + list.get(j) + " = " + target);
exactMatch = true;
}
}
}
}
if (!exactMatch) {
System.out.println(num1 + " * " + num2 + " = " + num1 * num2 + " ~ " + target);
}
}
</code></pre>
</div>
<h5 style="display: inline;">Solution: O(N) </h5>
<ul style="display: inline;" class="actions small">
<li style="display: inline;"><a id="nov-12-solution-fast-btn" onclick='displayCodeSnippet("nov-12-solution-fast","nov-12-solution-fast-btn");return false;' class="button small">Show</a></li>
<li><br></li>
</ul>
<div id="nov-12-solution-fast" style="display: none;">
<pre><code>
/**
* O(n) solution for finding the exact match using hashmap, implement V2 for most efficient closest factor calculation in O(nlogn)
*
* @param list contains the list of all numbers
* @param target is the value that the factors need to multiply out to or be the closest to
*/
public static void findClosestFactorV3(List< Integer> list, int target) {
Map< Integer, Integer> map = new HashMap<>();
for (int i = 0; i < list.size(); i++) {
if (map.get(list.get(i)) == null) {
map.put(list.get(i), 1);
} else {
map.replace(list.get(i), map.get(list.get(i)) + 1);
}
}
for (int i = 0; i < list.size(); i++) {
int div = target / list.get(i);
if (div * list.get(i) == target) {
if(map.get(div) != null) {
if(div == list.get(i) && map.get(div) > 1) {
System.out.println(list.get(i) + " * " + div + " = " + target);
}
if(div != list.get(i)) {
System.out.println(list.get(i) + " * " + div + " = " + target);
}
}
}
}
}
</code></pre>
</div>
<header class="major">
<span class="date">October 24, 2019</span>
<h3>Advanced Session<br>
✭ Lead - Sanjeev ✭</h3>
</header>
<h5>Activity: Turning Over A New Leaf</h5>
<p>Based on the number of people in this activity, make sure you use a cloth big enough for everyone to fit one. The challenge in this game is that with everyone standing on the piece of cloth must work together to flip the piece of cloth completely. There are only 2 rules that you must obey! One is no one can step outside the piece of cloth area. And finally, no one can climb up on top of each other, to preserve space. Everyone’s feet must be touching the piece of cloth at all times!</p>
<h5>Problem:</h5>
<p>Design a stack that supports push, pop, top and retrieving the minimum element in constant time</p>
<ul>
<li>push(x) → Push element x onto stack</li>
<li>pop() → Removes the element at the top of the stack</li>
<li>top() → Return the top element</li>
<li>getMin/90 → Retrieve the minimum element in the stack</li>
</ul>
<p><b><a href="https://drive.google.com/drive/folders/1DBzv2KcwaNDilpxZxJTsPQ9BMU8fBy-_">Starter Code</a></b></p>
<p>Given a string containing just the characters (, ), {, }, [, ] determine if the input string is valid by checking if all open brackets have a closing bracket and that they are present in the right order</p>
<p><b><a href="https://leetcode.com/problems/valid-parentheses/solution/">Solution</a></b></p>
<p>Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window</p>
<p><b><a href="https://leetcode.com/problems/moving-average-from-data-stream/">Solution</a></b></p>
<p>Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks. Tasks could be done without original order. Each task could be done in one interval. For each interval, CPU could finish one task or just be idle. However, there is a non-negative cooling interval <i>n</i> that means between two same tasks, there must be at least <i>n</i> intervals that CPU are doing different tasks or just be idle. You need to return the lease number of intervals the CPU will take to finish all the given tasks.</p>
<p><b><a href="https://leetcode.com/problems/task-scheduler/">Solution</a></b></p>
<header class="major">
<span class="date">October 10, 2019</span>
<h3>Advanced Session<br>
Lead - Gautam</h3>
</header>
<h5>Problem:</h5>
<p>Given a linked list, find the middle element of the linked list</p>
<pre><code>
public void middle(String str) {
}
</code></pre>
<p>Given a linked list, an int m, and an int n, return the sum of the mth element from beginning of the list and the nth element from the end of the list</p>
<pre><code>
public void sumMN(int m, int n) {
}
</code></pre>
<p>Given a linked list, segregate the list into evens and odds in such a way that the evens are listed before odds</p>
<pre><code>
public void seperateOddEven() {
}
</code></pre>
<header class="major">
<span class="date">October 8, 2019</span>
<h3>Novice Session<br>
Lead - Sivam</h3>
</header>
<p>Return an integer array of the values in the matrix in spiral order</p>
<span class="image fit"><img src="images/oct-8-19-q1.png" alt="" /></span>
<pre><code>
public int[] spiral(int[][] matrix) {
}
</code></pre>
<p>Find the longest consecutive sequence of numbers in an integer array</p>
<pre><code>
public int longestConsecutive(int[] nums) {
}
</code></pre>
<p>If you are given a sorted array of integers, return the sorted array of the squares of all values</p>
<pre><code>
public int sortedSquares(int[] A) {
}
</code></pre>
<p>Given a 2d array of characters, check if the given word exists</p>
<span class="image fit"><img src="images/oct-8-19-q4.png" alt="" /></span>
<pre><code>
public boolean wordSearch(char[][] board, String word) {
}
</code></pre>
<header class="major">
<span class="date">October 3, 2019</span>
<h3>Novice Session<br>
Lead - Atharv</h3>
</header>
<h5>Problem:</h5>
<p>Write a function to print the number of occurrences of a character in a string</p>
<pre><code>
public void listCountsOfCharacters(String str) {
}
</code></pre>
<p>Write a function that rotates an array k times to the right</p>
<pre><code>
public int[] rotated(int[] array, int k) {
}
</code></pre>
<p>Given two 2-D arrays, write a function that multiplies them</p>
<pre><code>
public int[][] multiplyMatrix(int[][] a, int[][] b) {
}
</code></pre>
<header class="major">
<span class="date">October 1, 2019</span>
<h3>Novice Session<br>
Lead - Harsha</h3>
</header>
<h5>Problem:</h5>
<p>Write a function to compute the sum of all prime numbers less than equal to a given integer N</p>
<pre><code>
public int getPrimeSum (int n) { {
}
</code></pre>
<h5 style="display: inline;">Solution: O(√N) </h5>
<ul style="display: inline;" class="actions small">
<li style="display: inline;"><a id="oct-1-1-solution-fast-btn" onclick='displayCodeSnippet("oct-1-1-solution-fast","oct-1-1-solution-fast-btn");return false;' class="button small">Show</a></li>
<li><br></li>
</ul>
<div id="oct-1-1-solution-fast" style="display: none;">
<pre><code>
public int getPrimeSum (int n) {
if( n== 0 || n==1) {
return 0;
}
List< Boolean> numbers = new ArrayList<>();
for (int i = 0; i < limit + 1; i++) {
numbers.add(true);
}
for (int i = 2; i < Math.sqrt(numbers.size()); i++) {
if (numbers.get(i)) {
int multiple = i * i;
while (multiple < numbers.size()) {
numbers.set(multiple, false);
multiple += i;
}
numbers.set(i, true);
}
}
int sum = 0;
for (int i = 2; i < numbers.size(); i++) {
if (numbers.get(i)) {
sum+=i;
}
}
return sum;
}
</code></pre>
</div>
<p>Write a function that returns if two strings are anagrams</p>
<pre><code>
public boolean isAnagram (String a, String b) {
}
</code></pre>
<h5 style="display: inline;">Solution: O(N) </h5>
<ul style="display: inline;" class="actions small">
<li style="display: inline;"><a id="oct-1-2-solution-fast-btn" onclick='displayCodeSnippet("oct-1-2-solution-fast","oct-1-2-solution-fast-btn");return false;' class="button small">Show</a></li>
<li><br></li>
</ul>
<div id="oct-1-2-solution-fast" style="display: none;">
<pre><code>
public boolean isAnagram (String a, String b) {
int n1 = a.length;
int n2 = b.length;
if (n1!= n2) {
return false;
}
Map< String, Integer> str1 = new HashMap<>();
//add letters and frequency to hashmap
for(int i = 0;i < a.length;i++) {
String letter1 = a.charAt(i) + "";
if(str1.get(letter1) == null) {
str1.put(letter1, 1);
}
else {
str1.replace(letter1, str1.get(i) + 1);
}
}
//check if letters are present
for(int i =0;i < b.length;i++) {
String letter1 = b.charAt(i) + "";
if(str1.get(letter1) == null || str1.get(letter1) == 0) {
return false;
}
else {
str1.replace(letter1, str1.get(letter1) - 1);
}
}
return true;
}
</code></pre>
</div>
<h5 style="display: inline;">Solution: O(Nlog(N)) </h5>
<ul style="display: inline;" class="actions small">
<li style="display: inline;"><a id="oct-1-2-solution-slow-btn" onclick='displayCodeSnippet("oct-1-2-solution-slow","oct-1-2-solution-slow-btn");return false;' class="button small">Show</a></li>
<li><br></li>
</ul>
<div id="oct-1-2-solution-slow" style="display: none;">
<pre><code>
public boolean isAnagram (String a, String b) {
int n1 = a.length;
int n2 = b.length;
if (n1!= n2) {
return false;
}
char [] str1 = a.toCharArray();
char [] str2 = b.toCharArray();
Arrays.sort(str1);
Arrays.sort(str2);
// Compare sorted strings
for (int i = 0; i < str1.length; i++)
if (str1[i] != str2[i]) {
return false;
}
}
return true;
}
</code></pre>
</div>
<p>Suppose you had <i>N</i> identical balls. One of them is slightly heavier and you are given a balance scale. Write a function to return the fewest number of times you have to use the scale to find the heavier ball</p>
<pre><code>
public int getMinUses (int n) {
}
</code></pre>
<h5 style="display: inline;">Solution: O(1) </h5>
<ul style="display: inline;" class="actions small">
<li style="display: inline;"><a id="oct-1-3-solution-fast-btn" onclick='displayCodeSnippet("oct-1-3-solution-fast","oct-1-3-solution-fast-btn");return false;' class="button small">Show</a></li>
<li><br></li>
</ul>
<div id="oct-1-3-solution-fast" style="display: none;">
<pre><code>
public int getMinUses (int n) {
return (int) Math.ceil(Math.log(n)/Math.log(3));
}
</code></pre>
</div>
</section>
</div>
<!-- Copyright -->
<div id="copyright">
<ul>
<li>© Codeavor</li>
<li>Design: Harsha Srikara <a href="https://html5up.net"> HTML5 UP</a></li>
</ul>
</div>
</div>
<!-- Scripts -->
<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/jquery.scrollex.min.js"></script>
<script src="assets/js/jquery.scrolly.min.js"></script>
<script src="assets/js/browser.min.js"></script>
<script src="assets/js/breakpoints.min.js"></script>
<script src="assets/js/util.js"></script>
<script src="assets/js/main.js"></script>
</body>
</html>