找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 2550|回复: 11
收起左侧

[Google] Google Interview Question for Dev L

[复制链接]

1163

主题

182

精华

3625

积分

神级会员

Rank: 7Rank: 7Rank: 7

积分
3625
发表于 1-27-2017 01:07 AM | 显示全部楼层 |阅读模式

亲!马上注册或者登录会查看更多内容!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

Given a random string (reasonable length L), knowing all possible font sizes (e.g. font Fn has min_width, max_width, min_height, max_height), knowing fixed screen size, find the max font size that can display said string

1080

主题

159

精华

3274

积分

神级会员

Rank: 7Rank: 7Rank: 7

积分
3274
发表于 1-27-2017 01:07 AM | 显示全部楼层

Assuming the random string forms some kind of natural language flow text with words and spaces and no formatting such as line breaks, paragraphs, headings, tables etc. and no word division (hyphenation) or maybe with the strategy would almost stay the same.

Basic strategy:
1) estimate a font size by guessing
2) Layout based on the estimate, correct into either direction

1) Estimate
- there are unknowns like word wraps, average character width, but line height is given by the font size, e.g. 1.5 times max_height
- assume average word size (in characters) that is either typical or by analyzing the given text
- assume average charachter width either typical or by analyzing given text
- from the given screen width and each font characteristics you can now estimate the number of lines which in return gives yout the hight needed.
- calculate this characteristics for each font if n is reasonable small or binary search the font size and pick the biggest where the desired height fits within the bounds given

2) now layout with real text and adjust font until it overlaps or fit... when the string is really random, you might, after running some samples be able to bypass the last step or only correct font size downwards in case it overlaps in rare cases... how ever the estimations might need to be language sensitive.

评分

参与人数 1金钱 +3 收起 理由
Sophia + 3 很给力!祝您面试工作学习顺利~

查看全部评分

回复 支持 反对

使用道具 举报

1075

主题

136

精华

3137

积分

神级会员

Rank: 7Rank: 7Rank: 7

积分
3137
发表于 1-27-2017 01:07 AM | 显示全部楼层

function magicFunction (char, font) {& v" }2 q* J5 Y, G& \3 K; N
	return font.getDimensions(char)  P5 S- E/ }0 s/ U( `
}
. O% v$ s8 c, G8 S7 D) |) V  O2 g
2 B1 l0 m3 z) y4 hvar fonts = [/* font1, font2, font3, .... */]  a/ L2 E" V! F$ D

" D; Y, F4 h- e6 z8 K2 H" Gmodule.exports = function (s, screenWidth, screenHeight) {8 ?& p" U6 ]9 R; N" e, {
	// Assume fonts are sorted largest to smallest...
- i1 m" z2 v7 R; z8 s% i4 `2 }4 R) `	for (var i = 0; i < fonts.length; ++i) {. F9 \+ Q7 H1 N2 i% |2 A
		var font = fonts
# k7 K( J- I& n. _, h- t		var screenRemainingHeight = screenHeight
$ B* C9 ]2 o7 o6 b		var screenRemainingWidth = screenWidth  n5 ]6 w  x0 }1 z3 C& f
, O! H% |% U/ b7 b
		var maxCharacterHeightLine = 0
/ `% ?- p- n% w0 O+ n$ B		var lineWidth = 0
7 w$ h% L  r  C8 H+ `		for (var j = 0; j < s.length; ) {
! ]1 J+ S) q/ |! U  s) D			var char = s[j]
% r3 O5 e0 g- c  }$ H' e; k			var d = magicFunction(char, font); x' n; W/ m6 R4 s, u+ D
			if (d.height <= screenRemainingHeight) {% _( f- S$ E( E; Y; [: u: k6 L
				maxCharacterHeightLine = max(maxCharacterHeightLine, d.height)+ W3 Q" Y# D7 |: ~0 E
				lineWidth += d.width: w% r( x6 P: e, M
				if (lineWidth > screenRemainingWidth) {
$ j- C+ c1 z: v/ ]% G; q& Z. V% N					// Start a new line5 m1 q6 @' l& i& H: U2 w( K
					// don't increment j
9 s2 R: F- j  {: s2 I0 s					lineWidth = 0
! _4 m# |: v5 O$ _$ K- K# ~  }					screenRemainingHeight -= maxCharacterHeightLine
2 M# p5 M" @3 C* J1 Q1 ?) ~				} else {: \( o# U2 _( D4 w/ s
					// it fits on this line, get next character in `s`
5 c4 l1 e; {! Z  m5 _					++j
5 H# q# B$ Y8 P, n3 q( C				}" d- t) [  F( M9 _
			} else {
  q0 u, W# j& b, {) J				// Decrease the font-size9 I, l7 |4 C# G" k2 _4 N- R
				break/ d, A. |8 F( x7 Z
			}
$ p2 E0 s% X2 {		}
8 ?: l8 \: W  }9 s$ r		if (j === s.length) {: _+ ^$ B. k4 T8 `! f$ ^3 ~
			return font.size
1 q  |) a- c6 C: q: c; r( D- d' Q. z% H		}
. J4 N$ ^4 c& e2 @" b/ E3 D% N	}2 D- g8 \1 H3 E+ v. y, ~3 m2 F
	return -1: o/ Y) v/ x; i9 j& b) f
}

评分

参与人数 1金钱 +2 收起 理由
Sophia + 2 赞一个!祝您面试工作学习顺利~

查看全部评分

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表