Zuora在线测试题解之Three Page Path
2016-06-02 14:36
267 查看
题目记不清楚了,大概是给出用户访问页面的记录(user, page),要求给出最经常被访问的前M个路径,每个路径至少包含n个页面。
定义类如下,测试通过。
1. 原始数据
2. 中间处理数据
3.处理后的数据
4.服务调用逻辑
祝后续的同学们好运!
定义类如下,测试通过。
1. 原始数据
package com.zuora.interview.path3page; public class UserPage { private String user; private String page; public UserPage() { // TODO Auto-generated constructor stub } public UserPage(String user, String page) { super(); this.user = user; this.page = page; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPage() { return page; } public void setPage(String page) { this.page = page; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub } }
2. 中间处理数据
package com.zuora.interview.path3page; public class UserPath { private String user; private String path; public UserPath() { // TODO Auto-generated constructor stub } public UserPath(String user, String path) { super(); this.user = user; this.path = path; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub } }
3.处理后的数据
package com.zuora.interview.path3page; public class PathCount { private String path; private int count; public PathCount() { // TODO Auto-generated constructor stub } public PathCount(String path, int count) { super(); this.path = path; this.count = count; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub } }
4.服务调用逻辑
package com.zuora.interview.path3page; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Solution { private List<UserPage> userPages = new ArrayList<UserPage>(); private List<UserPath> userPaths = new ArrayList<UserPath>(); private List<PathCount> pathCounts = new ArrayList<PathCount>(); public void initUserPages(){ userPages.add(new UserPage("U1", "/")); userPages.add(new UserPage("U1", "subscibers")); userPages.add(new UserPage("U2", "/")); userPages.add(new UserPage("U2", "subscibers")); userPages.add(new UserPage("U1", "filter")); userPages.add(new UserPage("U1", "export")); userPages.add(new UserPage("U2", "filter")); userPages.add(new UserPage("U2", "export")); userPages.add(new UserPage("U3", "/")); userPages.add(new UserPage("U3", "catalog")); userPages.add(new UserPage("U3", "edit")); } public void parseUserPage(){ for(UserPage up : userPages){ setUserPath(up); } } private void setUserPath(UserPage userPage){ if(userPage == null) return; boolean isUserExist = false; for(UserPath up : userPaths){ if(userPage.getUser().equals(up.getUser())){ isUserExist = true; up.setPath(up.getPath() + "->" + userPage.getPage()); break; } } if(!isUserExist){ userPaths.add(new UserPath(userPage.getUser(), userPage.getPage())); } } public void setPathNCount(int n){ if(n <= 0) return; for(UserPath up : userPaths){ String[] paths = up.getPath().split("->"); if(paths.length > n){ setPathNCount(paths, n); } } } private void setPathNCount(String[] path, int n){ if(n <= 0 || path == null || path.length < n) return; StringBuilder sb = new StringBuilder(); for(int i = 0; i <= path.length-n; i++){ for(int j = i; j < n+i; j++){ if(sb.length() == 0) sb.append(path[j]); else sb.append("->" + path[j]); } boolean isPathExist = false; for(PathCount pc : pathCounts){ if(sb.toString().equals(pc.getPath())){ isPathExist = true; pc.setCount(pc.getCount()+1); break; } } if(!isPathExist) pathCounts.add(new PathCount(sb.toString(), 1)); sb.delete(0, sb.length()); } } public void sortPathCounts(){ Collections.sort(pathCounts, new Comparator<PathCount> (){ public int compare(PathCount o1, PathCount o2){ if(o1.getCount() > o2.getCount()) return 1; return 0; } }); } public List<PathCount> getTopXPath(int x){ if (x < 0 || x > pathCounts.size()) return null; sortPathCounts(); List<PathCount> topX = new ArrayList<PathCount>(); for(int i = 0; i < x; i++){ topX.add(pathCounts.get(i)); } return topX; } public void print(List<PathCount> pc){ } /** * @param args */ public static void main(String[] args) { Solution sl = new Solution(); sl.initUserPages(); sl.parseUserPage(); sl.setPathNCount(3); List<PathCount> pc = sl.getTopXPath(2); sl.print(pc); } }
祝后续的同学们好运!
相关文章推荐
- 计算WWW在线人数
- 在线用表单建立文件夹
- 网站当前的在线人数
- 查看QQ是否在线状态的网页代码
- 基于jquery实现可定制的web在线富文本编辑器附源码下载
- 网页版在线聊天java Socket实现
- 戴维营教育2015年iOS暑期班笔试面试基础考核题
- Hadoop 1.2.1 安装笔记02: Java安装
- 2013阿里巴巴安全工程师实习生招聘笔试小结
- 个人开发者帐号+wireless install 实现非app store程序的在线更新功能
- 推荐一个免费的在线云计算操作系统Ghost。
- 用OWA访问Exchange邮箱:Exchange2003学习系列之五
- 接入与身份认证技术
- 程序员不可不知的7款编程工具
- Mysql Xtrabackup 详细介绍和使用
- zabbix学习(四)IT_Service管理
- zabbix学习(六)zabbix值映射Value mapping
- 可逆DNS(RDNS)反向解析简介
- 微信营销十九招 ! 转载
- D-Link DI-524M 无线由器限退