Created
January 1, 2014 07:44
-
-
Save t1anchen/8205998 to your computer and use it in GitHub Desktop.
Dummy Interview Problems
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Assert; | |
import org.junit.Before; | |
import org.junit.Test; | |
import java.util.*; | |
/** | |
* Dummy Interview 1: Sorting | |
* -------------------------- | |
* | |
* Given an array of integers, pls provide a way to print all the numbers that the count of digits equals n. | |
* And for every digit it is under ascending order from left to right. | |
* | |
* e.g. n = 3, print 123, 124, 125, ..., 189 | |
* | |
* ------------------------------- | |
* Created by au9ustine on 1/1/14. | |
*/ | |
public class DummyInterview001 { | |
private int i = 0; | |
private List<Integer> vec = new ArrayList<Integer>(); | |
private int k = 0; | |
private PriorityQueue<Integer> q = new PriorityQueue<Integer>(); | |
private Integer[] expected; | |
@Before | |
public void prepare() throws InterruptedException { | |
// suite initialization | |
q.clear(); | |
// expected data preparation | |
LinkedList<Integer> jlist = new LinkedList<Integer>(); | |
for (int j = 123; j < 189; j++) { | |
jlist.add(j); | |
} | |
expected = jlist.toArray(new Integer[0]); | |
// vec and k init | |
int j = 0; | |
while (j < expected.length) { | |
Random random = new Random(System.currentTimeMillis()); | |
int some = expected[random.nextInt(expected.length)]; | |
if (vec.contains(some) != true) { | |
vec.add(some); | |
j++; | |
} | |
} | |
k = vec.size(); | |
} | |
@Test | |
public void testSort() { | |
for (i = 0; i < k; i++) { | |
q.add(vec.get(i)); | |
} | |
int vec_index = 0; | |
while (i < vec.size()) { | |
vec.set(vec_index++, q.poll()); | |
q.add(vec.get(i++)); | |
} | |
while (q.size() > 0) { | |
vec.set(vec_index++, q.poll()); | |
} | |
Assert.assertArrayEquals(expected, vec.toArray(new Integer[0])); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment