Zuora在线测试题解之Three Page Path

题目记不清楚了,大概是给出用户访问页面的记录(user, page),要求给出最经常被访问的前M个路径,每个路径至少包含n个页面。

定义类如下,测试通过。


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);
	}

}

祝后续的同学们好运!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值