Interview Question


Country: India
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
0
of 0 vote

N == N ?

- summitzf June 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Scan on a line by line basis and keep on adding to a stack.
At the end, do N pop operations to print last N lines

- Koustav Chatterjee June 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;



public class Class1 {

	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub
		 File file = new File("C:\\Users\\yugandharr\\Desktop\\sample.txt");
	Scanner sc=new Scanner(file);
	String str="";
        int n=10;
	while(sc.hasNext()){
		
		str=sc.nextLine()+"\n"+str;
	}
	System.out.println(str);
	String[] LastN= str.split("\n",n);
		for(int i=n-1;i>=0;i--){
			System.out.println(LastN[i]);
		}
	}

}

- yugandharr147 June 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We are going to implement the stack using single linked list

- Anonymous June 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Have two pointers i and k. increment k from start till end but start incrementing i when k reaches N.
Once the file reading is completed, start printing the lines pointed by i till k-1.

package com.project.euler;

import java.io.File;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

public class FileReader {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			RandomAccessFile raf = new RandomAccessFile(new File("C:\\GameDevelopment\\eclipse\\artifacts.xml"), "r");
			List<String> lines = new ArrayList<String>();
			int N = 10;
			int i=0,k=0;
			String str;
			while((str = raf.readLine())!=null){
				if(k>=N){
					i++;
				}
				lines.add(str);
				k++;
			}
			
			while(i<k){
				System.out.println(lines.get(i++));
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

- AlgoAlgae July 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use a ring buffer.

- Anonymous July 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package crap;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

public class Printnthline {

	public static void main(String args[]) throws IOException {
		String line;
		int count = 0;
		LineNumberReader lnr = new LineNumberReader(new FileReader(
				"C:/Users/vinisha/Desktop/file.txt"));
		lnr.skip(Long.MAX_VALUE);
		System.out.println(lnr.getLineNumber());
		int value = lnr.getLineNumber();
		BufferedReader reader = new BufferedReader(new FileReader(
				"C:/Users/vinisha/Desktop/file.txt"));
		while ((line = reader.readLine()) != null) {
			// line = reader.readLine();
			count++;
			if (value+1 == count) {
				System.out.println("line:" + line);
			}

		}
	}

}

- Anonymous July 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException{
File f = new File("D:/New/Myfile.txt");
FileInputStream fs = new FileInputStream(f);
int c;
while((c = fs.read())!=-1)
{
System.out.print((char)c);
}
fs.close();
}
}

- chinnaraj October 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

very simple programe

import java.io.*;

public class fic {
public static void main(String[] args) throws IOException {

FileInputStream fi=new FileInputStream("D:/vamsi.txt");

InputStreamReader is=new InputStreamReader(fi);

BufferedReader bf=new BufferedReader(is);

while(bf.ready()){


String st=bf.readLine();

System.out.println(st);
}
}
}

- P.vamsikrishna February 13, 2015 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More