문제: 숫자 피라미드를 출력하세요 (1, 12, 123, ... 123...n)
public class PatternPrint {
public static void printPattern(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println();
}
}
public static void main(String[] args) {
printPattern(5);
// 출력:
// 1
// 12
// 123
// 1234
// 12345
}
}
문제: 입력된 숫자를 연산으로만 뒤집으세요 (예: 54321 → 12345)
public class NumberReverse {
public static int reverseNumber(int num) {
int reversed = 0;
while (num != 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
return reversed;
}
public static void main(String[] args) {
System.out.println(reverseNumber(54321)); // 출력: 12345
System.out.println(reverseNumber(1234)); // 출력: 4321
}
}
문제: 버블정렬을 구현하여 오름차순/내림차순 정렬하세요
import java.util.Arrays;
public class BubbleSort {
public static void bubbleSort(int[] arr, boolean ascending) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (ascending ? arr[j] > arr[j + 1] : arr[j] < arr[j + 1]) {
// swap
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
bubbleSort(arr, true); // 오름차순
System.out.println("오름차순: " + Arrays.toString(arr));
bubbleSort(arr, false); // 내림차순
System.out.println("내림차순: " + Arrays.toString(arr));
}
}
// 1. 배열에서 최댓값 찾기
function findMax(arr) {
return Math.max(...arr);
}
// 2. 문자열 뒤집기
function reverseString(str) {
return str.split('').reverse().join('');
}
// 3. 팰린드롬 확인
function isPalindrome(str) {
const cleaned = str.toLowerCase().replace(/[^a-z0-9]/gi, '');
return cleaned === cleaned.split('').reverse().join('');
}
// 4. 두 배열의 교집합
function intersection(arr1, arr2) {
return arr1.filter(x => arr2.includes(x));
}
// 5. 배열 중복 제거
function removeDuplicates(arr) {
return [...new Set(arr)];
}
// 예시 실행
console.log(findMax([1, 5, 3, 9, 2])); // 9
console.log(reverseString("hello")); // "olleh"
console.log(isPalindrome("A man a plan a canal Panama")); // true
console.log(intersection([1, 2, 3], [2, 3, 4])); // [2, 3]
function solution(N) {
const binary = N.toString(2);
let maxGap = 0;
let currentGap = 0;
let foundOne = false;
for (let i = 0; i < binary.length; i++) {
if (binary[i] === '1') {
if (foundOne) {
maxGap = Math.max(maxGap, currentGap);
}
foundOne = true;
currentGap = 0;
} else if (foundOne) {
currentGap++;
}
}
return maxGap;
}
// 테스트
console.log(solution(1041)); // 5 (10000010001 -> gap of 4 and 3, max is 5)
console.log(solution(9)); // 2 (1001 -> gap of 2)
function cyclicRotation(A, K) {
if (A.length === 0) return A;
const rotations = K % A.length;
return A.slice(-rotations).concat(A.slice(0, -rotations));
}
// 테스트
console.log(cyclicRotation([3, 8, 9, 7, 6], 3)); // [9, 7, 6, 3, 8]
function permMissingElem(A) {
const n = A.length + 1;
const expectedSum = (n * (n + 1)) / 2;
const actualSum = A.reduce((sum, num) => sum + num, 0);
return expectedSum - actualSum;
}
// 테스트
console.log(permMissingElem([2, 3, 1, 5])); // 4
-- 1. 테이블 조인 및 집계
SELECT d.department_name,
COUNT(e.employee_id) as employee_count,
AVG(e.salary) as avg_salary
FROM departments d
LEFT JOIN employees e ON d.department_id = e.department_id
GROUP BY d.department_id, d.department_name
ORDER BY employee_count DESC;
-- 2. 서브쿼리 활용 (평균 이상 급여자)
SELECT employee_name, salary
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);
-- 3. 윈도우 함수 활용 (급여 순위)
SELECT employee_name, salary,
RANK() OVER (ORDER BY salary DESC) as salary_rank
FROM employees;
// 추상클래스와 인터페이스를 활용한 도형 클래스 설계
abstract class Shape {
protected String color;
public Shape(String color) {
this.color = color;
}
public abstract double getArea();
public abstract double getPerimeter();
public String getColor() {
return color;
}
}
interface Drawable {
void draw();
}
class Circle extends Shape implements Drawable {
private double radius;
public Circle(String color, double radius) {
super(color);
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
@Override
public double getPerimeter() {
return 2 * Math.PI * radius;
}
@Override
public void draw() {
System.out.println("Drawing a " + color + " circle");
}
}
class Rectangle extends Shape implements Drawable {
private double width, height;
public Rectangle(String color, double width, double height) {
super(color);
this.width = width;
this.height = height;
}
@Override
public double getArea() {
return width * height;
}
@Override
public double getPerimeter() {
return 2 * (width + height);
}
@Override
public void draw() {
System.out.println("Drawing a " + color + " rectangle");
}
}
<!DOCTYPE html>
<html>
<head>
<style>
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.button {
padding: 10px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
.button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<div class="container">
<button class="button" onclick="handleClick()">클릭하세요</button>
</div>
<script>
function handleClick() {
alert('버튼이 클릭되었습니다!');
}
</script>
</body>
</html>
시간 배분[2]
핵심 포인트
이러한 문제들을 충분히 연습하면 스펙트라의 코딩테스트를 무난히 통과할 수 있을 것입니다.
인용:
[1] 취업후기 - 아이티윌 https://www.itwill.co.kr/cmn/board/BBSMSTR_000000000031/6321bbsDetail.do
[2] 기술면접모음-1 - Mint Flavor Chocolate DEV - 티스토리 https://sunghoon0307.tistory.com/7
[3] 코딜리티 Codility easy문제, permCheck swift 풀이 - MungGu Story https://0urtrees.tistory.com/275
[4] Codility: 연습문제 풀이 모음(Javascript) - 원석's 개발 블로그 https://songwonseok.github.io/algorithm/Codility/
[5][코딩테스트] JavaScript로 푸는 codility 1번 문제 풀이 https://hocheoljang.github.io/algorithm/algorithm/
[6][CODILITY] 코딜리티 문제풀이 파이썬 L3.TimeComplexity - 란 개발일지 https://juran-devblog.tistory.com/192
[7] javascript 알고리즘을 위한 기본 문법 - 36.5℃ https://dean30.tistory.com/95
[8] JAVA 자료구조부터 차근차근 코딩테스트 준비하기 로드맵 - 인프런 https://www.inflearn.com/roadmaps/2481
[9][JS] 알고리즘 기본 코드 정리 - velog https://velog.io/@hugh0223/JS-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98-%EA%B8%B0%EB%B3%B8-%EC%BD%94%EB%93%9C-%EC%A0%95%EB%A6%AC
[10][코딩 테스트 Java] 코딩 테스트 필수 문법 - 골든래빗 https://goldenrabbit.co.kr/2024/02/26/%EC%BD%94%EB%94%A9-%ED%85%8C%EC%8A%A4%ED%8A%B8-java-%EC%BD%94%EB%94%A9-%ED%85%8C%EC%8A%A4%ED%8A%B8-%ED%95%84%EC%88%98-%EB%AC%B8%EB%B2%95/
[11] JS] 검색과 검색 알고리즘(search algorithm) - 공부 기록일지 - 티스토리 https://blueprint-12.tistory.com/327
[12] java 코딩테스트 준비 (기본) - velog https://velog.io/@ydppwljg/java-%EC%BD%94%EB%94%A9%ED%85%8C%EC%8A%A4%ED%8A%B8-%EC%A4%80%EB%B9%84-%EA%B8%B0%EB%B3%B8
[13][codility] Lesson 1 | binary gap - 파이썬(Python) - 초보 개발자의 일기장 https://iambeginnerdeveloper.tistory.com/114
[14] Javascript - 알고리즘을 풀며 만난 자바스크립트 문법을 정리합니다 https://velog.io/@dev_cecy/series/Javascript
[15][ 코딩 테스트 예제 ] 쉬움(입문자)ver 모음집.zip - JJUNNAK's https://jjunnak.tistory.com/66
[16] 7가지 자바스크립트 알고리즘 - 국밥코딩 https://soup-in-rice-coding.tistory.com/8
[17][Java] 코딩테스트 기초 문법 - 개발일지 https://make-somthing.tistory.com/100
[18][CODILITY] 코딜리티 문제풀이 파이썬 L1.Iterations ~ L2.Arrays https://juran-devblog.tistory.com/191
[19][JS 알고리즘 메서드] JS알고리즘 공부 이유 & 기초메서드 정리 https://develaniper-devpage.tistory.com/54
[20] 코딩테스트 연습 - 프로그래머스 스쿨 https://school.programmers.co.kr/learn/challenges?order=acceptance_desc&levels=0&languages=java
[21] Codility 코딜리티 코딩 테스트(코테) 사이트 - 가입 및 사용법 https://inner-game.tistory.com/197
[22] 쉽게 설명한 자바스크립트 알고리즘 | 한상훈 - 교보문고 https://product.kyobobook.co.kr/detail/S000213996137