146 lines
3.6 KiB
TeX
146 lines
3.6 KiB
TeX
\documentclass[12pt,a4paper]{article}
|
|
\usepackage[utf8]{inputenc}
|
|
\usepackage{amsmath, amssymb}
|
|
\usepackage{geometry}
|
|
\usepackage{fancyhdr}
|
|
\usepackage{listings}
|
|
\usepackage{xcolor}
|
|
\usepackage{enumitem}
|
|
|
|
% Page layout
|
|
\geometry{margin=1in}
|
|
\pagestyle{fancy}
|
|
\fancyhf{}
|
|
\fancyhead[L]{Programming Competition Test}
|
|
\fancyhead[R]{\today}
|
|
\fancyfoot[C]{\thepage}
|
|
|
|
% Listings style for code
|
|
\lstset{
|
|
language=Java,
|
|
basicstyle=\ttfamily\small,
|
|
keywordstyle=\color{blue},
|
|
commentstyle=\color{green!60!black},
|
|
stringstyle=\color{red},
|
|
numbers=left,
|
|
numberstyle=\tiny,
|
|
stepnumber=1,
|
|
numbersep=5pt,
|
|
showstringspaces=false,
|
|
breaklines=true,
|
|
frame=single,
|
|
rulecolor=\color{black!30},
|
|
backgroundcolor=\color{black!2}
|
|
}
|
|
|
|
% Custom commands
|
|
\newcommand{\problem}[1]{\section*{#1}}
|
|
\newcommand{\inputformat}{\textbf{Input format:}}
|
|
\newcommand{\outputformat}{\textbf{Output format:}}
|
|
\newcommand{\examples}{\textbf{Example(s):}}
|
|
|
|
\begin{document}
|
|
|
|
\begin{center}
|
|
{\LARGE \textbf{Programming Competition Test}}\\[2mm]
|
|
{\large Duration: 2 hours \quad Total Marks: 100}
|
|
\end{center}
|
|
|
|
\section*{Instructions}
|
|
\begin{enumerate}[label=\arabic*.]
|
|
\item This test contains algorithmic problems similar to those found on LeetCode or HackerRank.
|
|
\item Use efficient algorithms to avoid timeouts; naive solutions may not receive full marks.
|
|
\item Submit solutions in C++, Python, or Java.
|
|
\item For each problem, write a function or program according to the specified input/output format.
|
|
\item Clearly comment your code if necessary.
|
|
\end{enumerate}
|
|
|
|
% ========================= Problem 1 =========================
|
|
\problem{Problem 1: Two Sum}
|
|
\textit{Difficulty: Easy}
|
|
|
|
Given an array of integers \texttt{nums} and an integer \texttt{target}, return indices of the two numbers such that they add up to \texttt{target}. Assume exactly one solution exists.
|
|
|
|
\inputformat
|
|
\begin{itemize}
|
|
\item First line contains an integer $n$ ($2 \leq n \leq 10^5$), the size of the array.
|
|
\item Second line contains $n$ integers, the elements of the array.
|
|
\item Third line contains the integer \texttt{target}.
|
|
\end{itemize}
|
|
|
|
\outputformat
|
|
\begin{itemize}
|
|
\item Print two integers, the indices of the elements adding up to \texttt{target}.
|
|
\end{itemize}
|
|
|
|
\examples
|
|
\begin{verbatim}
|
|
Input:
|
|
4
|
|
2 7 11 15
|
|
9
|
|
|
|
Output:
|
|
0 1
|
|
\end{verbatim}
|
|
|
|
% ========================= Problem 2 =========================
|
|
\problem{Problem 2: Longest Substring Without Repeating Characters}
|
|
\textit{Difficulty: Medium}
|
|
|
|
Given a string \texttt{s}, find the length of the longest substring without repeating characters.
|
|
|
|
\inputformat
|
|
\begin{itemize}
|
|
\item A single line containing the string \texttt{s} ($1 \leq |s| \leq 10^5$).
|
|
\end{itemize}
|
|
|
|
\outputformat
|
|
\begin{itemize}
|
|
\item A single integer, the length of the longest substring without repeated characters.
|
|
\end{itemize}
|
|
|
|
\examples
|
|
\begin{verbatim}
|
|
Input:
|
|
abcabcbb
|
|
|
|
Output:
|
|
3
|
|
\end{verbatim}
|
|
|
|
% ========================= Problem 3 =========================
|
|
\problem{Problem 3: Merge Intervals}
|
|
\textit{Difficulty: Hard}
|
|
|
|
Given a collection of intervals, merge all overlapping intervals.
|
|
|
|
\inputformat
|
|
\begin{itemize}
|
|
\item First line contains an integer $n$ ($1 \leq n \leq 10^4$), the number of intervals.
|
|
\item Next $n$ lines contain two integers each, representing the start and end of each interval.
|
|
\end{itemize}
|
|
|
|
\outputformat
|
|
\begin{itemize}
|
|
\item Print the merged intervals in ascending order of start times.
|
|
\end{itemize}
|
|
|
|
\examples
|
|
\begin{verbatim}
|
|
Input:
|
|
4
|
|
1 3
|
|
2 6
|
|
8 10
|
|
15 18
|
|
|
|
Output:
|
|
1 6
|
|
8 10
|
|
15 18
|
|
\end{verbatim}
|
|
|
|
\end{document}
|
|
|