Using the optimal fluctuation method, we evaluate the short-time probability distribution
P
(
H
ˉ
,
L
,
t
=
T
)
of the spatially averaged height
H
ˉ
=
(
1
/
L
)
∫
0
L
h
(
x
,
t
=
T
)
d
x
of a one-dimensional interface
h
(
x
,
t
)
governed by the Kardar–Parisi–Zhang equation
∂
t
h
=
ν
∂
x
2
h
+
λ
2
∂
x
h
2
+
D
ξ
x
,
t
on a ring of length L. The process starts from a flat interface,
h
(
x
,
t
=
0
)
=
0
. Both at
λ
H
ˉ
<
0
and at sufficiently small positive
λ
H
ˉ
the optimal (that is, the least-action) path
h
(
x
,
t
)
of the interface, conditioned on
H
ˉ
, is uniform in space, and the distribution
P
(
H
ˉ
,
L
,
T
)
is Gaussian. However, at sufficiently large
λ
H
ˉ
>
0
the spatially uniform solution becomes sub-optimal and gives way to non-uniform optimal paths. We study these, and the resulting non-Gaussian distribution
P
(
H
ˉ
,
L
,
T
)
, analytically and numerically. The loss of optimality of the uniform solution occurs via a dynamical phase transition of either first or second order, depending on the rescaled system size
ℓ
=
L
/
ν
T
, at a critical value
H
ˉ
=
H
ˉ
c
(
ℓ
)
. At large but finite
ℓ
the transition is of first order. Remarkably, it becomes an ‘accidental’ second-order transition in the limit of
ℓ
→
∞
, where a large-deviation behavior
−
ln
P
(
H
¯
,
L
,
T
)
≃
(
L
/
T
)
f
(
H
¯
)
(in the units
λ
=
ν
=
D
=
1
) is observed. At small
ℓ
the transition is of second order, while at
ℓ
=
O
(
1
)
transitions of both types occur.