Interview Question for SDE1s


Country: India
Interview Type: Written Test




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

/**
 * 
 */
package questions;

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

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.log4j.Log4j2;//Using lombok annotation for log4j handle

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.StringFormatterMessageFactory;

/**
 * 
 */
// Log4j Handle creator (from lombok)
@Log4j2
@Data
@EqualsAndHashCode(callSuper = false)
public class FindLinesWordsChars {
	static final Logger log = LogManager
			.getLogger(StringFormatterMessageFactory.INSTANCE);

	static int lineCount, wordCount, charCount;
	
	public static void main(String[] args) {
		log.debug("Starting..");
		try (BufferedReader lnr = new BufferedReader(
				new FileReader("c:/eula.1036.txt"), 2048)) {
			String line;
			while (lnr.ready() && (line = lnr.readLine()) != null) {
				lineCount++;
				String words[]=line.split(" ");
				wordCount+=words.length;
				for(String word:words)
					charCount+=word.length();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
		log.debug("lc=%d,wc=%d,cc=%d",lineCount, wordCount, charCount);

	}

}

- ts_v_murthy December 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
void main()
{
	FILE *f1;
	int char, word, line;
	while ((c = getc(f1)) != EOF) {
		if (c != '\0')
			char ++;
		else
			word++;
		if (c == '\n')
			line++;
	}
}

- sur December 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

#include <stdio.h>
void main()
{
        FILE *f1;
        int char, word, line;
        char c;
        while ((c = getc(f1)) != EOF) {
                if (c != '\0')
                        char ++;
                else
                        word++;
                if (c == '\n')
                        line++;
        }
        printf("char=%d word=%d line=%d", char, word, line)
}

- suraj December 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The if/else block appears to be wrong. Ignores all punctuation for the word count.

- nothing special here December 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int getwordcount(const string &str)
{
	int res = 0;
	string prestr = "";
	for(int i = 0; i < str.length(); ++i)
	{
		if(ispunctuationorseperatechar(str[i]))
		{
			if(prestr != "") res++;
			prestr = "";
		}
		else
		{
			prestr += s[i];
		}
	}
        return res;
}

void printCounts(string &filename)
{
	if(filename == "") return;
	int linecount = 0, wordcount = 0, charcount = 0;
	ifstream ifs(filename.c_str());
	while(!ifs.eof())
	{
		string line;
		getline(ifs, line);
		if(line.length() <= 1) continue;
		linecount++;
		charcount += line.length();
		wordcount += getwordcount(line);
	}
	ifs.close();
        cout << linecount<<","<<wordcount<<","<<charcount<<endl;
}

- busycai December 16, 2013 | 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