Samsung Electronics Coding Test — Recurring Implementation Modules (Java)

📅 Implementation modules in Java that repeatedly show up in Samsung-style simulation problems, collected during prep. Based on the codetree Samsung past-problem set.

Companion post — 5-attempt retrospective:

Reference: codetree — Samsung past-problem collection


1. Rotation — 90° rotation (CW / CCW)

codetree's Maze Runner · Artistry problems require 90° rotation of part of the grid.

⚠️ Not the full grid but a sub-region — work in relative coordinates from start/end.

int N = 5;
int[][] map = new int[N][N];
for (int i = 0; i < 5; i++) {
    map[i][0] = 1;
}

// Given an existing map.
int start_x = 0, end_x = N;
int start_y = 0, end_y = N;

// 90° clockwise rotation
int[][] newMap = new int[N][N];
for (int i = start_x; i < end_x; i++) {
    for (int j = start_y; j < end_y; j++) {
        newMap[start_x + (j - start_y)][(end_y - 1) - (i - start_x)] = map[i][j];
    }
}

Counter-clockwise — swap the index pattern:

newMap[(end_x - 1) - (j - start_y)][start_y + (i - start_x)] = map[i][j];

2. Periodic Boundary Condition

Wrap around to the opposite edge when crossing. codetree 'Turret Destruction'.

⚠️ 0-base vs 1-base changes the formula.

Step-by-step

// 0 ~ N-1 indexing
int new_x = (N + (x + dx[dir])) % N;
int new_y = (M + (y + dy[dir])) % M;

// 1 ~ N indexing (add 1 outside, subtract 1 inside)
int new_x = (N + ((x - 1) + dx[dir])) % N + 1;
int new_y = (M + ((y - 1) + dy[dir])) % M + 1;

Big jump

int distance = 150000000;

// 0 ~ N-1 indexing
int new_x = Math.abs((N + (x + distance))) % N;
int new_y = Math.abs((M + (y + distance))) % M;

// 1 ~ N indexing
int new_x = Math.abs((N + ((x - 1) + distance))) % N + 1;
int new_y = Math.abs((M + ((y - 1) + distance))) % M + 1;

Without Math.abs, negative coordinates break this. Pattern: handle negatives → add N to make positive → mod.

3. Reverse direction problems

Notably 'Dice Problem 3' and 'Rabbit Race'. This part needs understand-then-memorize — the direction-index mapping varies per problem.

Dice has a fixed top-bottom face mapping per rotation. Rabbit race has a directional inverse like (dir + 2) % 4 on a grid.

4. Sort — multi-condition

Recently common — 4–5 chained conditions.

Integer — standard Comparator

Collections.sort(list, new Comparator<Integer>() {
    @Override
    public int compare(Integer o1, Integer o2) { // positive → swap, negative → keep
        if (o1 < o2) return 1;       // descending
        else if (o1 == o2) return 0;
        else return -1;
    }
});

Simpler: return o1 - o2; (ascending) or return o2 - o1; (descending).

Long — use compareTo

⚠️ Direct subtraction on Long risks overflow. Use compareTo.

List<Long> list2 = new ArrayList<>(Arrays.asList(1L, 30L, 0L, 0L, 1L, 3L, 2L, 1L, 0L, 5L));
Collections.sort(list2, new Comparator<Long>() {
    @Override
    public int compare(Long o1, Long o2) {
        if (o1 == 0) {
            if (o2 == 0) return 0;
            else return 1;   // only o1 is 0 → push back
        } else if (o2 == 0) {
            return -1;       // only o2 is 0 → push back
        } else {
            return o1.compareTo(o2);
        }
    }
});

Multi-condition — chain by priority

Collections.sort(list, (a, b) -> {
    if (a.cond1 != b.cond1) return a.cond1 - b.cond1;       // 1st priority
    if (a.cond2 != b.cond2) return b.cond2 - a.cond2;       // 2nd, descending
    return a.cond3 - b.cond3;                                // 3rd
});

6. BFS — shortest path

Most shortest-path problems → BFS. When you need trace (path), keep a parent[] array. codetree 'Turret Destruction' is the canonical one.

⚠️ Plain BFS puts (coord, dist) on the queue. Trace requires storing prev coord alongside or via a separate parent[][].

7. Spiral — coordinates + direction storage

Baekjoon 'Shark and Blizzard', codetree 'Storm'.

⚠️ Coordinates alone aren't enough — many problems need direction (dir, rDir) stored too.

static void makeSpiral() {
    int[] dx = {0, 1, 0, -1};
    int[] dy = {-1, 0, 1, 0};

    int index = 0;
    int dir = 0, rDir = 2;
    int cur_depth = 0;
    int ref_depth = 1;
    int turn = 0;

    int x_0 = N / 2;
    int y_0 = N / 2;
    spiralMap.put(index, new Node(x_0, y_0, dir, rDir));

    while (true) {
        index++;
        if (index == N * N) break;

        int new_x = x_0 + dx[dir];
        int new_y = y_0 + dy[dir];

        spiralMap.put(index, new Node(new_x, new_y, dir, rDir));
        cur_depth++;

        if (cur_depth == ref_depth) {
            dir = (dir + 1) % 4;
            rDir = (dir + 2) % 4;
            cur_depth = 0;
            turn++;
            spiralMap.put(index, new Node(new_x, new_y, dir, rDir));
            if (turn == 2) {
                ref_depth++;
                turn = 0;
            }
        }

        x_0 = new_x;
        y_0 = new_y;
    }
}

Key: ref_depth increments every 2 direction changes. (1, 1, 2, 2, 3, 3, 4, 4, ...) pattern.


Traps I fell into — 3 of them

Trap 1 — Global condition vs local condition

The most important thing to watch while reading the problem. Does a condition apply to the entire simulation or just locally?

Examples: - 'Turret Destruction': "most recently attacked turret" vs "attack count" — easy to confuse. - 'Rabbit Race': "any rabbit that jumped within the K-loop window" vs "any rabbit that ever jumped in the entire simulation" — different sets.

Trap 2 — Concurrency (List.remove + for loop)

Removing inside a for loop skips one element. Fix by i-- after remove.

for (int i = 0; i < list.size(); i++) {
    if (cond) {
        list.remove(i);
        i--;  // ← compensate for skipped index
    }
}

Example: [1, 2, 3, 4, 5] → at index 2, remove 3[1, 2, 4, 5], but next index becomes 3, pointing at 5 instead of 4.

Hit this in 'Hide and Seek' too — forgot i--.

Trap 3 — Loop search needs break

'Tail Catch' — the head searches 4 directions for the next destination. Forgetting to break after finding overwrites the result with later directions.

4 3 2 1         4 3 2 1        4 3 2 2         4 3 3 2        4 4 3 2
4 0 0 4    →    4 0 0 1   →    4 0 0 1   →     4 0 0 1   →    4 0 0 1
4 4 4 4         4 4 4 4        4 4 4 4         4 4 4 4        4 4 4 4

Once target is found, break immediately — otherwise subsequent direction checks overwrite it.

for (int dir = 0; dir < 4; dir++) {
    if (cond) {
        // handle
        break;  // ← key
    }
}

Summary

  • 90° rotation: memorize sub-region coordinate transform.
  • PBC: 0-base vs 1-base · handle negatives · Math.abs + N for big distances.
  • Sort: use compareTo for Long · chain multi-conditions.
  • Spiral: direction + ref_depth pattern.
  • List.remove: don't forget i--.
  • Loop search: break immediately on find.
  • Condition scope: global vs local — biggest read-the-problem trap.

5-attempt retrospective companion post:


📦 Migrated from my own Korean blog (my own writing). Original: taehyuklee.tistory.com/17

Share𝕏f

Comments